返回介绍

wp_list_bookmarks()

发布于 2017-09-11 12:21:39 字数 10299 浏览 1287 评论 0 收藏 0

wp_list_bookmarks( string|array $args = '' )

Retrieve or echo all of the bookmarks.


description

List of default arguments are as follows:

These options define how the Category name will appear before the category links are displayed, if ‘categorize’ is 1. If ‘categorize’ is 0, then it will display for only the ‘title_li’ string and only if ‘title_li’ is not empty.


参数

$args

(string|array) (Optional) String or array of arguments to list bookmarks.

  • 'orderby'
    (string) How to order the links by. Accepts post fields. Default 'name'.
  • 'order'
    (string) Whether to order bookmarks in ascending or descending order. Accepts 'ASC' (ascending) or 'DESC' (descending). Default 'ASC'.
  • 'limit'
    (int) Amount of bookmarks to display. Accepts 1+ or -1 for all. Default -1.
  • 'category'
    (string) Comma-separated list of category ids to include links from.
  • 'category_name'
    (string) Category to retrieve links for by name.
  • 'hide_invisible'
    (int|bool) Whether to show or hide links marked as 'invisible'. Accepts 1|true or 0|false. Default 1|true.
  • 'show_updated'
    (int|bool) Whether to display the time the bookmark was last updated. Accepts 1|true or 0|false. Default 0|false.
  • 'echo'
    (int|bool) Whether to echo or return the formatted bookmarks. Accepts 1|true (echo) or 0|false (return). Default 1|true.
  • 'categorize'
    (int|bool) Whether to show links listed by category or in a single column. Accepts 1|true (by category) or 0|false (one column). Default 1|true.
  • 'show_description'
    (int|bool) Whether to show the bookmark descriptions. Accepts 1|true or 0|false. Default 0|false.
  • 'title_li'
    (string) What to show before the links appear. Default 'Bookmarks'.
  • 'title_before'
    (string) The HTML or text to prepend to the $title_li string. Default '<h2>'.
  • 'title_after'
    (string) The HTML or text to append to the $title_li string. Default '</h2>'.
  • 'class'
    (string) The CSS class to use for the $title_li. Default 'linkcat'.
  • 'category_before'
    (string) The HTML or text to prepend to $title_before if $categorize is true. String must contain '%id' and '%class' to inherit the category ID and the $class argument used for formatting in themes. Default '<li id="%id" class="%class">'.
  • 'category_after'
    (string) The HTML or text to append to $title_after if $categorize is true. Default '</li>'.
  • 'category_orderby'
    (string) How to order the bookmark category based on term scheme if $categorize is true. Default 'name'.
  • 'category_order'
    (string) Whether to order categories in ascending or descending order if $categorize is true. Accepts 'ASC' (ascending) or 'DESC' (descending). Default 'ASC'.

Default value: ''


返回值

(string|void) Will only return if echo option is set to not echo. Default is not return anything.


源代码

File: wp-includes/bookmark-template.php

function wp_list_bookmarks( $args = '' ) {
	$defaults = array(
		'orderby' => 'name', 'order' => 'ASC',
		'limit' => -1, 'category' => '', 'exclude_category' => '',
		'category_name' => '', 'hide_invisible' => 1,
		'show_updated' => 0, 'echo' => 1,
		'categorize' => 1, 'title_li' => __('Bookmarks'),
		'title_before' => '<h2>', 'title_after' => '</h2>',
		'category_orderby' => 'name', 'category_order' => 'ASC',
		'class' => 'linkcat', 'category_before' => '<li id="%id" class="%class">',
		'category_after' => '</li>'
	);

	$r = wp_parse_args( $args, $defaults );

	$output = '';

	if ( ! is_array( $r['class'] ) ) {
		$r['class'] = explode( ' ', $r['class'] );
	}
 	$r['class'] = array_map( 'sanitize_html_class', $r['class'] );
 	$r['class'] = trim( join( ' ', $r['class'] ) );

	if ( $r['categorize'] ) {
		$cats = get_terms( 'link_category', array(
			'name__like' => $r['category_name'],
			'include' => $r['category'],
			'exclude' => $r['exclude_category'],
			'orderby' => $r['category_orderby'],
			'order' => $r['category_order'],
			'hierarchical' => 0
		) );
		if ( empty( $cats ) ) {
			$r['categorize'] = false;
		}
	}

	if ( $r['categorize'] ) {
		// Split the bookmarks into ul's for each category
		foreach ( (array) $cats as $cat ) {
			$params = array_merge( $r, array( 'category' => $cat->term_id ) );
			$bookmarks = get_bookmarks( $params );
			if ( empty( $bookmarks ) ) {
				continue;
			}
			$output .= str_replace(
				array( '%id', '%class' ),
				array( "linkcat-$cat->term_id", $r['class'] ),
				$r['category_before']
			);
			/**
			 * Filters the bookmarks category name.
			 *
			 * @since 2.2.0
			 *
			 * @param string $cat_name The category name of bookmarks.
			 */
			$catname = apply_filters( 'link_category', $cat->name );

			$output .= $r['title_before'];
			$output .= $catname;
			$output .= $r['title_after'];
			$output .= "\n\t<ul class='xoxo blogroll'>\n";
			$output .= _walk_bookmarks( $bookmarks, $r );
			$output .= "\n\t</ul>\n";
			$output .= $r['category_after'] . "\n";
		}
	} else {
		//output one single list using title_li for the title
		$bookmarks = get_bookmarks( $r );

		if ( ! empty( $bookmarks ) ) {
			if ( ! empty( $r['title_li'] ) ) {
				$output .= str_replace(
					array( '%id', '%class' ),
					array( "linkcat-" . $r['category'], $r['class'] ),
					$r['category_before']
				);
				$output .= $r['title_before'];
				$output .= $r['title_li'];
				$output .= $r['title_after'];
				$output .= "\n\t<ul class='xoxo blogroll'>\n";
				$output .= _walk_bookmarks( $bookmarks, $r );
				$output .= "\n\t</ul>\n";
				$output .= $r['category_after'] . "\n";
			} else {
				$output .= _walk_bookmarks( $bookmarks, $r );
			}
		}
	}

	/**
	 * Filters the bookmarks list before it is echoed or returned.
	 *
	 * @since 2.5.0
	 *
	 * @param string $html The HTML list of bookmarks.
	 */
	$html = apply_filters( 'wp_list_bookmarks', $output );

	if ( ! $r['echo'] ) {
		return $html;
	}
	echo $html;
}

更新日志

Versiondescription
2.1.0Introduced.

相关函数

Uses

  • wp-includes/l10n.php: __()
  • wp-includes/functions.php: wp_parse_args()
  • wp-includes/taxonomy.php: get_terms()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/bookmark-template.php: _walk_bookmarks()
  • wp-includes/bookmark-template.php: link_category
  • wp-includes/bookmark-template.php: wp_list_bookmarks
  • wp-includes/bookmark.php: get_bookmarks()
  • Show 3 more uses Hide more uses

Used By

  • wp-includes/deprecated.php: wp_get_links()
  • wp-includes/deprecated.php: wp_get_linksbyname()
  • wp-includes/widgets/class-wp-widget-links.php: WP_Widget_Links::widget()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by Codex

    Simple List

    Displays all bookmarks with the title “Bookmarks” and with items wrapped in tags. The title is wrapped in h2 tags.

    
    <?php wp_list_bookmarks('title_li=&category_before=&category_after='); ?>
    
  2. Simple List without the Heading

    Displays all bookmarks as above, but does not include the default heading.

    
    <?php wp_list_bookmarks('title_li=&categorize=0'); ?>
    

    Specific Category Sorted by URL

    Displays bookmarks for Category ID 2 in span tags, uses images for bookmarks, does not show descriptions, sorts by bookmark URL.

    
    <?php wp_list_bookmarks('categorize=0&category=2&before=<span>&after=</span>&show_images=1&show_description=0&orderby=url'); ?>
    

    Shows Ratings and Timestamp

    Displays all bookmarks in an ordered list with descriptions on a new line, does not use images for bookmarks, sorts by bookmark id, shows ratings and last-updated timestamp (Note that the last updated timestamp does not track local modifications. It tracks when whatever the link points to is updated via remote requests to pingomatic.)

    
    <ol>
    <?php wp_list_bookmarks('between=<br />&show_images=0&orderby=id&show_rating=1&show_updated=1'); ?>
    </ol>
    

    Replaces Heading with Image

    Uses an image from the theme folder instead of plain text.

    
    <?php wp_list_bookmarks('categorize=0&title_before=&title_after=&title_li=<img src="'.get_bloginfo("stylesheet_directory").'/images/blogroll.gif" alt="blogroll" />'); ?>
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文