使用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
相关产品推荐
相关产品推荐

