返回介绍

get_the_category()

发布于 2017-09-11 00:32:28 字数 7723 浏览 1077 评论 0 收藏 0

get_the_category( int $id = false )

Retrieve post categories.


description

This tag may be used outside The Loop by passing a post id as the parameter.

Note: This function only returns results from the default "category" taxonomy. For custom taxonomies use get_the_terms().


参数

$id

(int) (Optional) default to current post ID. The post ID.

Default value: false


返回值

(array) Array of WP_Term objects, one for each category assigned to the post.


源代码

File: wp-includes/category-template.php

function get_the_category( $id = false ) {
	$categories = get_the_terms( $id, 'category' );
	if ( ! $categories || is_wp_error( $categories ) )
		$categories = array();

	$categories = array_values( $categories );

	foreach ( array_keys( $categories ) as $key ) {
		_make_cat_compat( $categories[$key] );
	}

	/**
	 * Filters the array of categories to return for a post.
	 *
	 * @since 3.1.0
	 * @since 4.4.0 Added `$id` parameter.
	 *
	 * @param array $categories An array of categories to return for the post.
	 * @param int   $id         ID of the post.
	 */
	return apply_filters( 'get_the_categories', $categories, $id );
}

更新日志

Versiondescription
0.71Introduced.

相关函数

Uses

  • wp-includes/category-template.php: get_the_terms()
  • wp-includes/category-template.php: get_the_categories
  • wp-includes/category.php: _make_cat_compat()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/load.php: is_wp_error()

Used By

  • wp-includes/category-template.php: get_the_category_list()
  • wp-includes/deprecated.php: the_category_ID()
  • wp-includes/deprecated.php: the_category_head()
  • wp-includes/link-template.php: get_permalink()
  • wp-includes/feed.php: get_the_category_rss()

User Contributed Notes

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

    Show the First Category Name Only

    
    $categories = get_the_category();
    
    if ( ! empty( $categories ) ) {
    	echo esc_html( $categories[0]->name );	
    }
    

    (Echoes the first array element ([0]) of $categories.)

    Make the first category link to the category page:

    
    $categories = get_the_category();
    if ( ! empty( $categories ) ) {
    	echo '<a href="' . esc_url( get_category_link( $categories[0]->term_id ) ) . '">' . esc_html( $categories[0]->name ) . '</a>';
    }
    
  2. Example response from function:

    $categories = get_the_category();
    
    var_dump($categories);
    array(1) {
    [0]=>
      object(stdClass)#310 (17) {
        ["term_id"]=>
        &int(6)
        ["name"]=>
        &string(10) "familylife"
        ["slug"]=>
        &string(10) "familylife"
        ["term_group"]=>
        int(0)
        ["term_taxonomy_id"]=>
        int(6)
        ["taxonomy"]=>
        string(8) "category"
        ["description"]=>
        &string(0) ""
        ["parent"]=>
        &int(0)
        ["count"]=>
        &int(208)
        ["object_id"]=>
        int(7729)
        ["filter"]=>
        string(3) "raw"
        ["cat_ID"]=>
        &int(6)
        ["category_count"]=>
        &int(208)
        ["category_description"]=>
        &string(0) ""
        ["cat_name"]=>
        &string(10) "familylife"
        ["category_nicename"]=>
        &string(10) "familylife"
        ["category_parent"]=>
        &int(0)
      }
    }

    Get the post category if you have a custom post_type

    
    <?php
    /* FIRST
     * Note: This function only returns results from the default “category” taxonomy. For custom taxonomies use get_the_terms().
     */
    $categories = get_the_terms( $post->ID, 'taxonomy' );
    // now you can view your category in array:
    // using var_dump( $categories );
    // or you can take all with foreach:
    foreach( $categories as $category ) {
        echo $category->term_id . ', ' . $category->slug . ', ' . $category->name . '<br />';
    }
    

    Show All Categories as Links
    This outputs all the categories assigned to the post as links. Must be used inside the loop. You can also use the function get_the_category_list() for this.

    
    $categories = get_the_category();
    $separator = ' ';
    $output = '';
    if ( ! empty( $categories ) ) {
    	foreach( $categories as $category ) {
    		$output .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" alt="' . esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ) . '">' . esc_html( $category->name ) . '</a>' . $separator;
    	}
    	echo trim( $output, $separator );
    }
    

    Get the Post Categories From Outside the Loop

    
    <?php
    $post = get_post();
    if ( $post ) {
    	$categories = get_the_category( $post->ID );
    	var_dump( $categories );
    }
    

    Show Category Images
    This outputs category images named after the cat_ID with the alt attribute set to cat_name. You can also use any of the other member variables instead.

    
    <?php
    $categories = get_the_category();
    foreach ( $categories as $category ) { 
    	echo '<img src="' . esc_url( 'http://example.com/images/' . intval( $category->term_id ) . '.jpg' ) . '" alt="' . esc_attr( $category->name ) . '" />'; 
    }
    

    Display all categories with name and description

    
    <div>
    <?php 
    	foreach((get_the_category()) as $category){
    		echo $category->name."<br>";
    		echo category_description($category);
    		}
    	?>
    </div>
    

    To display a list of categories associated with a post, separated by commas, write this code:


    $cats = array();
    foreach (get_the_category($post_id) as $c) {
    $cat = get_category($c);
    array_push($cats, $cat->name);
    }

    if (sizeOf($cats) > 0) {
    $post_categories = implode(', ', $cats);
    } else {
    $post_categories = 'Not Assigned';
    }

    echo $post_categories;

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

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

发布评论

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