如何在 WordPress 模板中获取父类别名称?我可以按父类别查询帖子吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1228130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:31:06  来源:igfitidea点击:

How do I get the parent category name in WordPress template? And can I query post by the parent category?

wordpresspostparentcategories

提问by

I tried getting help on the WordPress forums but no luck. Anyways, here is my question...

我尝试在 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

假设我正在为每个父级创建 10 个父类别和 2 个子类别。我的 WordPress 帖子属于特定父类别的一个子类别

How do I get the parent category name ONLY? I don't want subcategories names? what WordPress code would do that?

如何仅获取父类别名称?我不想要子类别名称?什么 WordPress 代码会做到这一点?

And one more question...

还有一个问题……

Is it possible to query post by the parent of a sub category by using:

是否可以使用以下方法查询子类别的父级的帖子:

but instead of entering cat=1or the name of the particular category, can I do something like:

但不是输入cat=1特定类别的名称,而是我可以执行以下操作:

So this way it would automatically insert and query post for the parent of any particular sub category that's clicked on?

所以这样它会自动插入和查询被点击的任何特定子类别的父级的帖子?

回答by Blago

To get the parent category name, use the get_cat_name()function, with the parent as the parameter -- like so:

要获取父类别名称,请使用该get_cat_name()函数,以父类别作为参数——像这样:

$cat = get_the_category();
$parentCatName = get_cat_name($cat[0]->parent);

回答by pixeline

All these answers failed for me.

所有这些答案对我来说都失败了。

I eventually managed to display a post's topmost category name like this :

我最终设法像这样显示帖子的最顶层类别名称:

        $categories = get_the_category();
        $category= '';
        foreach($categories as $childcat) {
            $parentcat = $childcat->category_parent;
            if($parentcat>0){
                $category = get_cat_name($parentcat);
                continue;
             }
        }
        $category = (strlen($category)>0)? $category :  $categories[0]->cat_name;

回答by lancemonotone

Found this answer, which gives you the first ancestor slug. It could easily be modified to give you the name.

找到了这个答案,它为您提供了第一个祖先蛞蝓。它可以很容易地修改为给你的名字。

Got it here: http://nick.boldison.com/wordpress/wordpress-get-top-level-parent-category/

在这里得到它:http: //nick.boldison.com/wordpress/wordpress-get-top-level-parent-category/

<?php
// get parent category slug
$parentCatList = get_category_parents($cat,false,',');
$parentCatListArray = split(",",$parentCatList);
$topParentName = $parentCatListArray[0];
$sdacReplace = array(" " => "-", "(" => "", ")" => "");
$topParent = strtolower(strtr($topParentName,$sdacReplace));
?>

In fact, to get the parent name:

实际上,要获取父名称:

// get parent category slug
$parentCatList = get_category_parents($cat,false,',');
$parentCatListArray = split(",",$parentCatList);
$topParentName = $parentCatListArray[0];

回答by markratledge

Lots of answers and examples in the Wordpress docs:

Wordpress 文档中有很多答案和示例:

get category parents

获取类别父母

get the category

获取类别

(And it looks like some code snips or other text didn't come through in your original question)

(看起来您的原始问题中没有出现一些代码片段或其他文本)