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

时间:2015-12-01 13:46:00

标签: php regex wordpress feed atom-feed

我想从自定义wordpress Feed中删除所有html标记。可能吗?怎么做?我尝试使用str.replace(/<\/?[^>]+>/gi, ''),但它根本没有帮助我。这是我用来创建自定义Feed的代码。我正在使用Yoast SEO插件。

<?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 个答案:

答案 0 :(得分:0)

正如Qirel提到我已取代

<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>

        <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>

谢谢