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

如何为WordPress分类标题启用短代码?

解决WordPress分类归档标题短代码解析问题

你的问题核心在于:分类归档页面的标题并非通过the_title钩子处理,而是由get_the_archive_title()函数生成,所以你之前添加的the_title过滤器覆盖不到这个场景。

下面是具体解决步骤:

1. 注册所需的年份/月份短代码

先在主题functions.php中添加短代码定义,按需选择:

// 注册当前年份短代码(如2024)
add_shortcode('current_year', function() {
    return date('Y');
});

// 注册当前月份短代码(数字格式,如08)
add_shortcode('current_month_num', function() {
    return date('m');
});

// 注册当前月份短代码(全称格式,如"八月")
add_shortcode('current_month_name', function() {
    return date('F');
});

2. 添加归档标题的短代码解析过滤器

替换你之前的代码,添加针对归档标题的专用过滤器:

add_filter('get_the_archive_title', 'parse_shortcode_in_archive_title');
function parse_shortcode_in_archive_title($title) {
    return do_shortcode($title);
}

3. 配置分类标题

进入WordPress后台编辑目标分类,在「分类名称」或主题提供的「归档标题」设置项中插入短代码,比如:科技资讯 - [current_year]年[current_month_name]

特殊情况排查

如果以上代码仍不生效,检查主题模板文件(如category.php或archive.php):

  • 确认标题输出使用的是echo get_the_archive_title();,而非硬编码内容或自定义函数
  • 若主题用了自定义归档标题函数,找到对应输出位置,在输出前调用do_shortcode()处理标题内容

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 22:39:58