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

如何为WordPress自定义文章类型添加Rank Math SEO的article:tag元标签?

解决Rank Math无法为自定义文章类型标签添加article:tag元标签的问题

可以通过WordPress的wp_head钩子手动输出这些元标签,具体操作如下:

  • 打开主题目录下的functions.php文件,或是创建一个自定义功能插件
  • 添加以下代码,根据你的实际场景替换占位符:
add_action('wp_head', function() {
    // 仅在目标自定义文章类型的单篇页面执行
    if (!is_singular('your_custom_post_type')) return;

    // 获取自定义标签字段的值,替换成你的字段名
    $custom_tags = get_post_meta(get_the_ID(), 'your_custom_tag_field', true);
    
    // 如果标签是以逗号分隔的字符串,先拆分成数组
    if (is_string($custom_tags)) {
        $custom_tags = array_map('trim', explode(',', $custom_tags));
    }

    // 循环输出每个标签的meta标签
    if (!empty($custom_tags) && is_array($custom_tags)) {
        foreach ($custom_tags as $tag) {
            echo '<meta property="article:tag" content="' . esc_attr($tag) . '">' . "\n";
        }
    }
});

代码说明:

  1. is_singular('your_custom_post_type'):将your_custom_post_type替换为你实际使用的自定义文章类型slug(比如product、portfolio)
  2. get_post_meta(...):将your_custom_tag_field替换为你自定义标签字段的键名
  3. 如果你的自定义标签字段本身存储的是数组而非字符串,可以移除字符串转数组的代码块
  4. esc_attr($tag):对标签内容进行转义处理,避免XSS安全风险

添加代码后,刷新自定义文章类型的单页页面,就能在<head>标签内看到对应的article:tag元标签了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 20:20:56