Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
175 views
in Technique[技术] by (71.8m points)

php - wordpress if post is in_category display category name

I want to check if a Wordpress post has category 'X, Y or Z' and if so display the name(s) of the category ('X Y or Z').

I already check and get a true of the category, but how can I show each name that has been found in_category();

if ( in_category( array( 'X', 'Y', 'Z') {
echo "yes";
}

How can I display the category name?

question from:https://stackoverflow.com/questions/65598351/wordpress-if-post-is-in-category-display-category-name

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

the simple answer to your question is this

get_the_category();

Using your example you can say

if (in_category( array('X', 'Y', 'Z') )) {
    echo get_the_category();
}

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

For more information and usage of get_the_category() visit the next link


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...