WordPress函数:wp_list_bookmarks 显示所有的友情链接

江河/ 2023年05月05日/ WordPress/ 浏览 552

友情链接功能在 WordPress 中默认是关闭的。如何开启友情链接功能,请参考:《WordPress怎么开启友情链接功能》


函数原型:


wp_list_bookmarks( string|array $args = '' ): void|string


显示所有的友情链接。


参数说明:


orderby,按什么排序。接受链接的所有字段:'id', 'link_id', 'name', 'link_name', 'url', 'link_url', 'visible', 'link_visible', 'rating', 'link_rating', 'owner', 'link_owner', 'updated', 'link_updated', 'notes', 'link_notes', 'description', 'link_description', 'length' and 'rand'。默认为:'name'。另外:'length' 表示按 'link_name'值的长度排序。


order,升序 ASC 或 降序 DESC,默认 ASC。


limit,-1 表示全部,1+整数表示要显示的个数。


category,以逗号分隔的类别ID列表,其中包含来自的链接。


category_name,要按名称检索链接的类别。


hide_invisible,是否显示或隐藏标记为“不可见”的链接。接受1|true或0|false。默认值1|true。


show_updated,是否显示书签上次更新的时间。接受1|true或0|false。默认值0|false。


echo,是回显还是返回格式化的书签。接受1|true(回显)或0|false(返回)。默认值1|true。


categorize,是显示按类别列出的链接,还是显示单列中的链接。接受1|true(按类别)或0|false(一列)。默认值1|true。


show_description,是否显示书签说明。接受1|true或0|false。默认值0|false。


title_li,链接出现之前要显示的内容。默认的“书签”。


title_before,要在$title_li字符串前附加的HTML或文本。默认值 <h2>。


title_after,要附加到$title_li字符串中的HTML或文本。默认</h2>。


class,用于$title_li的CSS类或类数组。默认“linkcat”。


category_before,如果$category为true,则在$title_before之前要准备的HTML或文本。字符串必须包含“%id”和“%class”,才能继承类别id和用于设置主题格式的$class参数。默认<li id=“%id”class=“%class”>。


category_after,如果$category为true,则要附加到$title_after的HTML或文本。默认</li>。


category_orderby,如果$category为true,如何根据术语方案对书签类别进行排序。默认的“名称”。


category_order,如果$category为true,则按升序还是降序排列类别。接受“ASC”(升序)或“DESC”(降序)。默认为“ASC”。


函数源码:


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>',
	);

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

	$output = '';

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

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

	if ( $parsed_args['categorize'] ) {
		// Split the bookmarks into ul's for each category.
		foreach ( (array) $cats as $cat ) {
			$params    = array_merge( $parsed_args, array( 'category' => $cat->term_id ) );
			$bookmarks = get_bookmarks( $params );
			if ( empty( $bookmarks ) ) {
				continue;
			}
			$output .= str_replace(
				array( '%id', '%class' ),
				array( "linkcat-$cat->term_id", $parsed_args['class'] ),
				$parsed_args['category_before']
			);

			$catname = apply_filters( 'link_category', $cat->name );

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

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

	$html = apply_filters( 'wp_list_bookmarks', $output );

	if ( $parsed_args['echo'] ) {
		echo $html;
	} else {
		return $html;
	}
}


包含钩子:


apply_filters( 'link_category', string $cat_name )

apply_filters( 'wp_list_bookmarks', string $html )


使用举例:


<?php wp_list_bookmarks('title_li=&category_before=&category_after='); ?>


发表评论

暂无评论,抢个沙发...

客服 工单