Display a post's category within the wordpress loop?

前端 未结 4 699
不知归路
不知归路 2021-02-11 07:19

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:57

    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; ?>
        
    
    
    

提交回复
热议问题