get_bookmark()根据链接ID获取链接相关信息

目录

描述

译文

根据书签链接ID检索书签信息。

原文

Retrieve Bookmark data based on bookmark link ID.

get_bookmark() 描述

用法

<?php get_bookmark$bookmark$output$filter ?>

get_bookmark() 用法

参数

$bookmark

(integer|object) (必填) Bookmark link ID or Bookmark object.

默认值: None

$output

(string) (可选) Either OBJECT, ARRAY_N, or ARRAY_A constant

默认值: OBJECT

$filter

(string) (可选) default is 'raw'.

默认值: 'raw'

get_bookmark() 参数

返回值

(array|object) 

Type returned depends on $output value.

get_bookmark() 返回值

示例

Display Bookmark Name

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php 
$bookmark = get_bookmark(5);
echo $bookmark->link_name; 
?>

or:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php echo get_bookmark(5)->link_name; ?>

Display Bookmark as a Link

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php  
$bookmark = get_bookmark(5);
echo '<a href="'.$bookmark->link_url.'">'.$bookmark->link_name.'</a>';
?>

get_bookmark() 示例

注意

  • 使用到 global: (object) $wpdb Database Object

get_bookmark() 注意

历史

添加于 版本: 2.1.0

get_bookmark() 历史

源文件

get_bookmark() 函数的代码位于 wp-includes/bookmark.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Retrieve Bookmark data
 *
 * @since 2.1.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int|stdClass $bookmark
 * @param string $output Optional. Either OBJECT, ARRAY_N, or ARRAY_A constant
 * @param string $filter Optional, default is 'raw'.
 * @return array|object|null Type returned depends on $output value.
 */
function get_bookmark($bookmark, $output = OBJECT, $filter = 'raw') {
	global $wpdb;

	if ( empty($bookmark) ) {
		if ( isset($GLOBALS['link']) )
			$_bookmark = & $GLOBALS['link'];
		else
			$_bookmark = null;
	} elseif ( is_object($bookmark) ) {
		wp_cache_add($bookmark->link_id, $bookmark, 'bookmark');
		$_bookmark = $bookmark;
	} else {
		if ( isset($GLOBALS['link']) && ($GLOBALS['link']->link_id == $bookmark) ) {
			$_bookmark = & $GLOBALS['link'];
		} elseif ( ! $_bookmark = wp_cache_get($bookmark, 'bookmark') ) {
			$_bookmark = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->links WHERE link_id = %d LIMIT 1", $bookmark));
			if ( $_bookmark ) {
				$_bookmark->link_category = array_unique( wp_get_object_terms( $_bookmark->link_id, 'link_category', array( 'fields' => 'ids' ) ) );
				wp_cache_add( $_bookmark->link_id, $_bookmark, 'bookmark' );
			}
		}
	}

	if ( ! $_bookmark )
		return $_bookmark;

	$_bookmark = sanitize_bookmark($_bookmark, $filter);

	if ( $output == OBJECT ) {
		return $_bookmark;
	} elseif ( $output == ARRAY_A ) {
		return get_object_vars($_bookmark);
	} elseif ( $output == ARRAY_N ) {
		return array_values(get_object_vars($_bookmark));
	} else {
		return $_bookmark;
	}
}

get_bookmark() 源文件

相关