Wordpress:完全替换the_content

问题描述 投票:0回答:1

我有一个新闻帖的集合,我添加了一个guid(帖子ID的sha1),我想在feed中包含标签(使其更容易在网页上显示)。

但是当我更新'description'标签时,它只是将它添加到响应中,而不是将其删除,所以我在json_encode之后得到以下结果:

"description": [
    "header subheader the original text with no tags",    // want to remove if possible.
    {
       "h1": "header",
       "h3": "subheader",
       "p": [
           "the original text",
           "with no tags",
       ]
    }

]

这是我的代码:

add_action('rss2_item', function(){
  global $post;

  $output = '';
  $output .= '<id>' . sha1($post->ID) . '</id>';
  $output .= '<description>' . wpautop( $post->post_content ) . '</description>';

  echo $output;
});
php xml wordpress rss feed
1个回答
0
投票

动作rss2_item只允许您向现有订阅源项添加节点,您无法删除节点:

在rss2提要的默认输出之后立即触发rss2_item操作挂钩。

您需要的可能是自定义Feed模板,可以使用钩子do_feed_rss2实现。来自Codex

function my_custom_rss() {

    if ( 'photos' === get_query_var( 'post_type' ) ) {
        get_template_part( 'feed', 'photos' );
    } 
    else {
        get_template_part( 'feed', 'rss2' );
    }
}
remove_all_actions( 'do_feed_rss2' );
add_action( 'do_feed_rss2', 'my_custom_rss', 10, 1 );
© www.soinside.com 2019 - 2024. All rights reserved.