【问题标题】:How To Get Parent Category Slug of Current Post如何获取当前帖子的父类别信息
【发布时间】:2014-01-26 18:05:51
【问题描述】:

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

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

【问题讨论】:

  • 发布您目前尝试过的代码。

标签: wordpress parent


【解决方案1】:

您可以尝试类似的方法来查找帖子的顶级类别。 帖子也应该与类别和子类别相关联。

//get all categories of current post
$categories = get_the_category($post->ID);

//get top level category of current post
$top_cat_obj = array();
foreach($categories as $cat) {

    if ($cat->parent == 0) {
        $top_cat_obj[] = $cat;  
    }

}           
$top_cat_name = $top_cat_obj[0]->name;
$top_cat_slug = $top_cat_obj[0]->slug;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 2017-07-22
    • 1970-01-01
    • 1970-01-01
    • 2021-07-10
    • 1970-01-01
    相关资源
    最近更新 更多