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

使用apple_news_exporter_title后,如何修改Apple News JSON输出的title字段?

修改Apple News导出JSON中title字段的最优方案

问题背景

使用apple_news_exporter_title钩子后,页面显示的Apple News标题组件已更新,但导出的JSON根节点title字段仍保留WordPress原文章标题,无法满足Apple审核时的标题差异化需求。

最优解决方法

直接使用Apple News官方提供的apple_news_article_json钩子——这个钩子专门用于修改最终生成的完整JSON数据,能直接覆盖根节点的title字段,是官方推荐的修改JSON核心字段的方式。

代码实现

将以下代码添加到你的主题functions.php文件或自定义插件中:

add_filter( 'apple_news_article_json', 'custom_apple_news_json_title', 10, 2 );
function custom_apple_news_json_title( $json, $post ) {
    // 替换为你的自定义标题逻辑,比如从自定义字段获取Apple News专属标题
    $apple_news_title = get_post_meta( $post->ID, 'apple_news_custom_title', true );
    
    // 如果有自定义标题则使用,否则保留原标题(可选逻辑)
    if ( ! empty( $apple_news_title ) ) {
        $json['title'] = sanitize_text_field( $apple_news_title );
    }
    
    return $json;
}

补充说明

  • apple_news_article_json钩子接收两个参数:$json是完整的Apple News文章JSON数组,$post是当前文章对象
  • 你可以根据需求调整标题逻辑:比如添加固定前缀/后缀、调用自定义字段、或者动态生成适配Apple News的标题
  • 测试时可通过Apple News插件的文章编辑页导出JSON,确认title字段已成功更新

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 21:33:14