如何在 wordpress 提要中显示没有 html 标签的内容?
How to show content without html tags in feed of wordpress?
我想从我的自定义 wordpress 提要中删除所有 html 标签。可能吗?怎么做?我尝试使用 str.replace(/<\/?[^>]+>/gi, '')
但它根本帮不了我。这是我用来创建自定义提要的代码。我正在使用 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>
正如 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>
谢谢
我想从我的自定义 wordpress 提要中删除所有 html 标签。可能吗?怎么做?我尝试使用 str.replace(/<\/?[^>]+>/gi, '')
但它根本帮不了我。这是我用来创建自定义提要的代码。我正在使用 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>
正如 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>
谢谢