Display a post's category within the wordpress loop?

前端 未结 4 1934
我在风中等你
我在风中等你 2021-02-11 07:07

Okay, so I have a Wordpress template that I created that only displays posts that have the \"workout\" category associated with it. Within the loop that displays those, I want t

4条回答
  •  故里飘歌
    2021-02-11 07:50

    Get the category objects:

    $cats = get_the_category($id);
    

    Just echo the name:

    echo $cats[0]->name;
    

    If you want to output a link, use this:

    
        name; ?>
    
    

    Note: instead of wp_get_post_categories($id), you could just use get_the_category().


    Update: if you want to display all the categories, just loop through them:

    
    
        
            name; ?>
        
    
    
    

提交回复
热议问题