如何在WordPress模板中获取父类别名称?我可以按父类别查询帖子吗? [英] How do I get the parent category name in WordPress template? And can I query post by the parent category?
问题描述
我尝试在WordPress论坛上获得帮助,但没有运气。无论如何,这是我的问题...
I tried getting help on the WordPress forums but no luck. Anyways, here is my question...
可以说我要为每个父级创建10个父类别和2个子类别。我的WordPress帖子属于特定父类别的一个子类别
Lets say I am creating 10 parent categories and 2 sub categories to each parent. My WordPress post belongs to one sub category of a particular parent category
如何仅获取父类别名称?我不需要子类别名称?
How do I get the parent category name ONLY? I don't want subcategories names? what WordPress code would do that?
还有一个问题...
是否可以查询帖子?由子类别的父项使用:
Is it possible to query post by the parent of a sub category by using:
,而不是输入 cat = 1
或特定类别的名称,我可以做类似的事情:
but instead of entering cat=1
or the name of the particular category, can I do something like:
这样,它将自动插入并查询帖子
So this way it would automatically insert and query post for the parent of any particular sub category that's clicked on?
推荐答案
要获得父类别名称,请使用 get_cat_name()
函数,将父级作为参数-像这样:
To get the parent category name, use the get_cat_name()
function, with the parent as the parameter -- like so:
$cat = get_the_category();
$parentCatName = get_cat_name($cat[0]->parent);
这篇关于如何在WordPress模板中获取父类别名称?我可以按父类别查询帖子吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!