【问题标题】:Get the parent taxonomy for custom post type获取自定义帖子类型的父分类
【发布时间】:2019-11-04 21:34:38
【问题描述】:

我有一个名为“listing”的自定义帖子类型和一个名为“listing-category”的自定义分类

我正在尝试为 single-listing.php 帖子创建一个类,该类仅是父自定义分类的 slug,例如:

留下 - 酒店 - 民宿

吃 - 咖啡馆 - 面包店

购物 - 食物 - 鞋子

因此,对于酒店下的单个帖子 - 我需要创建一个“住宿”类,因为该类下的所有帖子都将具有相同的样式。

我希望输出的是例如:

<div id="main-content" class="stay">

上一个问题似乎最接近,但我似乎无法让它适用于我的项目:https://wordpress.stackexchange.com/questions/24794/get-the-the-top-level-parent-of-a-custom-taxonomy-term

我已经在这个圈子里转了大约两天,所以任何帮助都将不胜感激!

【问题讨论】:

    标签: wordpress custom-post-type custom-taxonomy hierarchical


    【解决方案1】:

    实际上,您提供链接的答案是可以的,并且也可以解决您的问题。可能是您执行错误。

    这是您的案例的示例代码:

    global $post;
    $ctp_listing_cats=wp_get_post_terms( $post->ID, 'listing-category' );
    
    if (!empty($ctp_listing_cats[0]->term_id)){
       $topparent=get_term_top_most_parent($ctp_listing_cats[0]->term_id,'listing-category');
    }
    
    //...
    echo '<div id="main-content" class="'.$topparent->name.'">';
    

    get_term_top_most_parent() 给定代码的函数是来自您提供链接的另一个答案的函数。

    【讨论】:

    • 我不知道自己在做什么 - 我尝试了很多方法来让前面的示例正常工作,但是使用您的示例代码是否可以正常工作 - 非常感谢 :-)!跨度>
    猜你喜欢
    • 2023-04-05
    • 2016-10-28
    • 2023-04-03
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-23
    • 2014-04-29
    相关资源
    最近更新 更多