Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

categories - Wordpress - Get Current Category Parents

In category.php, how would I test against a category having a parent category?

If the parent category is A, and the sub-category is B, and the user loads the URL for category B, I would like to be able to test if it has parent A, and run code if it does.

I've found the get_category_parents tag, but it seems to return a link list rather than an array:

get_category_parents($cat, TRUE, ', '));

Even if I got the array, I'm not sure what the php function is to test against it (php noob).

Thanks!

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You can use the cat_is_ancestor_of function to check if a category is a child of another category. For example to check if the current category is a child of a category named 'blog':

cat_is_ancestor_of(get_cat_id('blog'), get_query_var('cat'))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...