You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用get_the_category()并按菜单顺序输出文章所属分类?

解决方案

该需求完全可以实现。

你当前使用的get_the_category()函数默认按分类ID升序输出结果,没有提供自定义排序参数,因此无法直接输出按菜单顺序排列的分类列表。

你可以替换为wp_get_post_terms()函数实现需求,该函数支持指定按分类的菜单顺序(对应term_order字段)排序,修改后的代码如下:

// 获取当前文章的分类,按菜单顺序排序
$categories = wp_get_post_terms( get_the_ID(), 'category', [
    'orderby' => 'term_order',
    'order'   => 'ASC', // 如需降序排列可将参数值改为DESC
    'hide_empty' => false
] );

foreach ( $categories as $category ) {
    if ( $category->term_id !== 14 ) {
        echo esc_html( $category->name );
    }
}

内容的提问来源于stack exchange,提问作者monsaic123

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 15:15:04