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

如何实现WordPress标签页标题动态化,在标题前展示对应标签文章数量

你使用的the_title过滤器仅作用于页面内容区域输出的单篇文章标题,无法作用于标签归档页的全局标题,可根据需求选择对应方案实现:

方案1:仅修改浏览器标签栏的标签页标题

add_filter( 'document_title_parts', 'custom_tag_title_with_post_count' );
function custom_tag_title_with_post_count( $title_parts ) {
    if ( is_tag() ) {
        $tag = get_queried_object();
        $post_count = $tag->count;
        // 可按需调整数字和标题之间的分隔符
        $title_parts['title'] = $post_count . ' ' . $title_parts['title'];
    }
    return $title_parts;
}

方案2:同时修改浏览器标题 + 页面内显示的标签归档标题

如果需要页面顶部展示的标签名称前也携带文章数量,额外添加以下代码即可:

add_filter( 'get_the_archive_title', 'custom_tag_archive_title_with_count' );
function custom_tag_archive_title_with_count( $title ) {
    if ( is_tag() ) {
        $tag = get_queried_object();
        $post_count = $tag->count;
        $title = $post_count . ' ' . $title;
    }
    return $title;
}

注意:你原有代码中使用了中文全角引号,需替换为英文半角引号否则会触发语法错误。

内容的提问来源于stack exchange,提问作者Mohammad Mahdi Aghakhani

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 12:18:04