get_the_category_list()获取所有分类,并以html的形式显示

目录

描述

Retrieve category list in either HTML list or custom format. Generally used for quick, delimited (eg: comma-separated) lists of categories, as part of a post's entry meta. For a more powerful, list-based function, see wp_list_categories().

get_the_category_list() 描述

用法

<?php get_the_category_list$separator$parents$post_id ); ?>

get_the_category_list() 用法

参数

$separator

(string) (可选) Optional, default is empty string. Separator for between the categories.

默认值: empty string

$parents

(string) (可选) Optional. How to display the parents. Values: 'multiple', 'single', empty string

默认值: empty string

$post_id

(int) (可选) Optional. Post ID to retrieve categories.

默认值: false

get_the_category_list() 参数

返回值

(string

get_the_category_list() 返回值

示例

Implementation in WordPress default Twenty Eleven theme

In content-single.php:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

<?php
	/* translators: used between list items, there is a space after the comma */
	$categories_list = get_the_category_list( __( ', ', 'twentyeleven' ) );

	/* translators: used between list items, there is a space after the comma */
	$tag_list = get_the_tag_list( '', __( ', ', 'twentyeleven' ) );
	if ( '' != $tag_list ) {
		$utility_text = __( 'This entry was posted in %1$s and tagged %2$s by <a href="%6$s">%5$s</a>. Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'twentyeleven' );
	} elseif ( '' != $categories_list ) {
		$utility_text = __( 'This entry was posted in %1$s by <a href="%6$s">%5$s</a>. Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'twentyeleven' );
	} else {
		$utility_text = __( 'This entry was posted by <a href="%6$s">%5$s</a>. Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'twentyeleven' );
	}

	printf(
		$utility_text,
		$categories_list,
		$tag_list,
		esc_url( get_permalink() ),
		the_title_attribute( 'echo=0' ),
		get_the_author(),
		esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) )
	);
?>

Display as List Items

By leaving the $separator value empty, it will generate an unordered list instead, complete with classes.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

<?php echo get_the_category_list(); ?>

Result:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

<ul class="post-categories">
	<li>
		<a href="http://myblog.com/category/business" title="View all posts in Business" rel="category tag">Business</a>
	</li>
</ul>

get_the_category_list() 示例

注意

Categories are ordered by name regardless of the parent-child category relationship. See this example from wp_list_categories() to display the Post categories with the category relationship intact.

get_the_category_list() 注意

历史

添加于 版本: 1.5.1

get_the_category_list() 历史

源文件

get_the_category_list() 函数的代码位于 wp-includes/category-template.php

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * 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 .= '';
	} 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 );
}

get_the_category_list() 源文件

相关