1. 首页
  2. WordPress 函数手册

get_the_category_list()

get_the_category_list( string $separator = , string $parents = , int $post_id = false )

以HTML列表或自定义格式检索文章的类别列表。
Retrieve category list for a post in either HTML list or custom format.

目录锚点:#参数#返回#源码#笔记


参数(Parameters)

参数 类型 必填 说明
$separator (string) 可选 类别之间的分隔符。默认情况下,链接被放置在无序列表中。空字符串将导致默认行为。
$parents (string) 可选 如何显示父对象。
$post_id (int) 可选 发布ID以检索类别。

返回(Return)

(string)


源码(Source)

/**
 * Retrieve category list in either HTML list or custom format.
 *
 * @since 1.5.1
 *
 * @global WP_Rewrite $wp_rewrite
 *
 * @param string $separator Optional, default is empty string. Separator for between the categories.
 * @param string $parents Optional. How to display the parents.
 * @param int $post_id Optional. Post ID to retrieve categories.
 * @return string
 */
function get_the_category_list( $separator = '', $parents='', $post_id = false ) {
	global $wp_rewrite;
	if ( ! is_object_in_taxonomy( get_post_type( $post_id ), 'category' ) ) {
		/** This filter is documented in wp-includes/category-template.php */
		return apply_filters( 'the_category', '', $separator, $parents );
	}

	$categories = get_the_category( $post_id );
	if ( empty( $categories ) ) {
		/** This filter is documented in wp-includes/category-template.php */
		return apply_filters( 'the_category', __( 'Uncategorized' ), $separator, $parents );
	}

	$rel = ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) ? 'rel="category tag"' : 'rel="category"';

	$thelist = '';
	if ( '' == $separator ) {
		$thelist .= '';
		foreach ( $categories as $category ) {
			$thelist .= "
	";
			switch ( strtolower( $parents ) ) {
				case 'multiple':
					if ( $category->parent )
						$thelist .= get_category_parents( $category->parent, true, $separator );
					$thelist .= '' . $category->name.'';
					break;
				case 'single':
					$thelist .= '';
					if ( $category->parent )
						$thelist .= get_category_parents( $category->parent, false, $separator );
					$thelist .= $category->name.'';
					break;
				case '':
				default:
					$thelist .= '' . $category->name.'';
			}
		}
		$thelist .= '';
	} else {
		$i = 0;
		foreach ( $categories as $category ) {
			if ( 0 < $i="" )="" $thelist="" .="$separator;" switch="" (="" strtolower(="" $parents="" )="" )="" {="" case="" 'multiple':="" if="" (="" $category-="">parent )
						$thelist .= get_category_parents( $category->parent, true, $separator );
					$thelist .= '' . $category->name.'';
					break;
				case 'single':
					$thelist .= '';
					if ( $category->parent )
						$thelist .= get_category_parents( $category->parent, false, $separator );
					$thelist .= "$category->name";
					break;
				case '':
				default:
					$thelist .= '' . $category->name.'';
			}
			++$i;
		}
	}

	/**
	 * Filter the category or list of categories.
	 *
	 * @since 1.2.0
	 *
	 * @param array  $thelist   List of categories for the current post.
	 * @param string $separator Separator used between the categories.
	 * @param string $parents   How to display the category parents. Accepts 'multiple',
	 *                          'single', or empty.
	 */
	return apply_filters( 'the_category', $thelist, $separator, $parents );
}
更新版本 源码位置 使用 被使用
1.5.1 wp-includes/category-template.php:132 1 function 11

笔记(Notes)

显示为列表项
WordPress默认二十一主题的实现

get_the_category_list() 为WP2原创文章,链接:https://www.wp2.cn/functions/get_the_category_list/

发表评论

您的电子邮箱地址不会被公开。