dongxingguo1978 2015-12-01 13:46
浏览 43
已采纳

如何在wordpress的feed中显示没有html标签的内容?

I want to remove all html tags from my custom wordpress feed. Is it possible? How to do it? I tried to use str.replace(/<\/?[^>]+>/gi, '') but it doesn't help me at all. Here is the code i used to create custom feed. I am using Yoast SEO Plugin.

<?php
/*
Template Name: Custom Feed
*/

$numposts = 10;

function yoast_rss_date( $timestamp = null ) {
  $timestamp = ($timestamp==null) ? time() : $timestamp;
  echo date(DATE_RSS, $timestamp);
}

function yoast_rss_text_limit($string, $length, $replacer = '...') { 
  $string = strip_tags($string);
  if(strlen($string) > $length) 
    return (preg_match('/^(.*)\W.*$/', substr($string, 0, $length+1), $matches) ? $matches[1] : substr($string, 0, $length)) . $replacer;   
  return $string; 
}

function rss_noiframe($content) {
    $content = preg_replace( '/<iframe(.*)\/iframe>/is', '', $content );
    $content = preg_replace( '/<script(.*)\/script>/is', '', $content );
    return $content;
}

add_filter('the_excerpt_rss', 'rss_noiframe');
add_filter('the_content_feed', 'rss_noiframe');


$posts = query_posts('showposts='.$numposts.'&cat=7');

$lastpost = $numposts - 1;

header("Content-Type: application/rss+xml; charset=UTF-8");
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>


<rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
 xmlns:dc="http://purl.org/dc/elements/1.1/"
 xmlns:media="http://search.yahoo.com/mrss/"
 xmlns:atom="http://www.w3.org/2005/Atom"
 xmlns:georss="http://www.georss.org/georss">

<channel>
  <title>My Website Feed</title>
  <link>http://www.mywebsite.com/</link>
  <description>The latest blog posts from mywebsite.com.</description>
  <language>en-us</language>
  <pubDate><?php yoast_rss_date( strtotime($ps[$lastpost]->post_date_gmt) ); ?></pubDate>
  <lastBuildDate><?php yoast_rss_date( strtotime($ps[$lastpost]->post_date_gmt) ); ?></lastBuildDate>
  <managingEditor>admin@mywebsite.com (Admin Admin)</managingEditor>
 <atom:link href='http://pubsubhubbub.superfeedr.com/' rel='hub' /> 
<atom:link href="http://www.mywebsite.com/feed/" rel="self" type="application/rss+xml" />

<?php foreach ($posts as $post) { ?>
    <?php
        global $post;
        $author_id=$post->post_author;
    ?>

    <item>
            <title><?php echo get_the_title($post->ID); ?></title>
        <dc:creator><?php the_author_meta( 'user_nicename', $author_id ); ?></dc:creator>
            <link><?php echo get_permalink($post->ID); ?></link>
            <description><?php echo '<![CDATA['.rss_noiframe($post->post_content).'<br/><br/>Keep on reading: <a href="'.get_permalink($post->ID).'">'.get_the_title($post->ID).'</a>'.']]>';  ?></description>
        <pubDate><?php yoast_rss_date( strtotime($post->post_date_gmt) ); ?></pubDate>
            <guid><?php echo get_permalink($post->ID); ?></guid>
<?php
$categories = get_the_category();
foreach ( $categories as $category ) { 
?>      
        <category><?php echo '<![CDATA['.$category->name.']';  ?></category> <?php } ?>


    </item>
<?php } ?>

</channel>
</rss>
  • 写回答

1条回答 默认 最新

  • douyu0852 2015-12-01 14:52
    关注

    As Qirel mention I replaced

    <description><?php echo '<![CDATA['.rss_noiframe($post->post_content).'<br/><br/>Keep on reading: <a href="'.get_permalink($post->ID).'">'.get_the_title($post->ID).'</a>'.']]>';  ?></description>
    

    TO

            <description><?php echo '<![CDATA['.strip_tags(rss_noiframe($post->post_content)).'<br/><br/>Keep on reading: <a href="'.get_permalink($post->ID).'">'.get_the_title($post->ID).'</a>'.']]>';  ?></description>
    

    Thank You

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题