如何获取顶级分类名称?

如何获取顶级分类名称?


标签: 

正在查看 5 个帖子:1-5 (共 5 个帖子)
  • 作者
    帖子
  • eoeore
    参与者
    • 文章数量: 114
    @eoeore
    楼主

    请问自定义内容有2级分类,在某个页面的详情页,怎么显示该页面的顶级分类名称
    在循环内容里,显示该页面的顶级分类名称。

    是用get_the_terms( int|WP_Post $post, string $taxonomy )  ??

    因为不懂代码,如果不复杂的话,能否提供代码?

    诗语
    管理员
    • 文章数量: 5,862
    @feibisi

    今天商城订单有点多,需要人手处理下,忙完给你找找相关说明。

    诗语
    管理员
    • 文章数量: 5,862
    @feibisi

    看下这个是不是你要的那种

    https://wordpress.stackexchange.com/questions/45083/get-the-first-post-term

    下面是 get_the_terms 的官方示例,

    https://developer.wordpress.org/reference/functions/get_the_terms/

    不是很清楚你说的自定义内容是不是指自定义文章类型 CPT 如果是的话你可以用一些热门的开发工具来做的。

    eoeore
    参与者
    • 文章数量: 114
    @eoeore
    楼主

    非常感谢工作这么晚,还没休息,还在帮忙解答。

    我的咨询是:

    如果在文章有二级分类,在文章详细页,如果获取该篇文章的顶级(父级)分类名称?

    eoeore
    参与者
    • 文章数量: 114
    @eoeore
    楼主

    刚刚通过论坛搜索,已经找到解答了,非常感谢哈。
    可以获取文章或自定义内容,详细页的顶级分类名称或子类名称。

    https://wordpress.stackexchange.com/questions/331879/get-categorys-parent-category-while-using-get-the-category-list

    $taxonomy = 'category'; //如果是自定义内容,这里修改自定义分类名称。
    
    // Get the term assigned to post.
    $post_term = wp_get_object_terms( $post->ID, $taxonomy);
    
    if ( ! empty( $post_term ) && ! is_wp_error( $post_term ) ) {
    
    // parent category object
    $parent = get_term($post_term[0]->parent, $taxonomy);
    
    echo '
    <div class="cat-links">
    
    ';
    
    get_template_part('images/inline', 'picto-actu.svg');
    
    // Display post categories.
    echo '<a href="'. get_term_link($parent->term_id,$taxonomy).'">'.$parent->name.'</a> >';
    echo '<a href="'. get_term_link($post_term[0]->term_id,$taxonomy).'">'.$post_term[0]->name.'</a> >';
    
    echo '
    
    </div>
    ';
    
    }
正在查看 5 个帖子:1-5 (共 5 个帖子)
  • 哎呀,回复话题必需登录。

未找到对应子版块?请直接在 [疑难杂症] 版块中发帖提问。

收藏纪念品


有奖活动

参与论坛社区问答,有机会获赠文派瓦普(Wapuu.com)手办模型。

话题信息

商业系统