WordPress的-获取当前类别的父母 [英] Wordpress - Get Current Category Parents

查看:61
本文介绍了WordPress的-获取当前类别的父母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在category.php中,我如何针对具有父类别的类别进行测试?

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

如果父类别为A,子类别为B,并且用户加载了类别B的URL,我希望能够测试它是否具有父A,并运行它(如果有)。

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.

get_category_parents标记,但它似乎返回链接列表而不是数组:

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, ', '));

即使我得到了数组,我也不知道针对它进行测试的php函数是什么(php noob)。

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

谢谢!

推荐答案

您可以使用 cat_is_ancestor_of 函数来检查类别是另一类的孩子。例如,检查当前类别是否是名为博客的类别的子代:

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'))

这篇关于WordPress的-获取当前类别的父母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆