【问题标题】:Remove HTML from PHP String - Wordpress RSS Feed Template从 PHP 字符串中删除 HTML - Wordpress RSS 源模板
【发布时间】:2018-06-23 08:56:37
【问题描述】:

感谢大家的帮助;目前还没有任何工作,所以添加更多细节。

我在functions.php中使用下面的函数来允许我使用我自己的RSS模板,它工作正常。

add_action('init', 'customRSS');
function customRSS(){
        add_feed('shows', 'customRSSFunc');
}
function customRSSFunc(){
        get_template_part('rss', 'shows');
}

feed 的 php 模板是:

<?php
header('Content-Type: ' . feed_content_type('rss2') . '; charset=' . get_option('blog_charset'), true);
$more = 1;
echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>';
do_action( 'rss_tag_pre', 'rss2' );
?>
<rss version="2.0"
    xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:wfw="http://wellformedweb.org/CommentAPI/"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:atom="http://www.w3.org/2005/Atom"
    xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
    xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
    <?php do_action( 'rss2_ns' );?>>

<channel>
    <title><?php wp_title_rss(); ?></title>
    <atom:link href="<?php self_link(); ?>" rel="self" type="application/rss+xml" />
    <link><?php bloginfo_rss('url') ?></link>
    <description><?php bloginfo_rss("description") ?></description>
    <lastBuildDate><?php
        $date = get_lastpostmodified( 'GMT' );
        echo $date ? mysql2date( 'r', $date, false ) : date( 'r' );
    ?></lastBuildDate>
    <language><?php bloginfo_rss( 'language' ); ?></language>
    <sy:updatePeriod><?php
        $duration = 'hourly';
        echo apply_filters( 'rss_update_period', $duration );
    ?></sy:updatePeriod>
    <sy:updateFrequency><?php
        $frequency = '1';
        echo apply_filters( 'rss_update_frequency', $frequency );
    ?></sy:updateFrequency>
    <?php do_action( 'rss2_head'); while( have_posts()) : the_post();?>
    <item>
        <title><?php the_title_rss(); ?></title>
        <date><?php echo customevent_get_start_date( null, false, 'd-m-Y' ); ?></date>
        <time><?php echo customevent_get_start_date( null, false, 'H:i' ); ?></time>
        <price><?php echo customevent_get_cost(); ?></price>
        <link><?php the_permalink_rss(); ?></link>
        <imageurl><?php the_post_thumbnail_url(); ?></imageurl>
        <description>
            <?php
            $content = the_content();
            echo $content ; ?>
        </description>
        <?php rss_enclosure(); ?>
        <?php do_action('rss2_item'); ?>
    </item>
<?php endwhile; ?>
</channel>
</rss>

我遇到问题的部分是描述 (the_content)。提要上的输出显示 p 标签等,以及在许多位置。两者都需要剥离。

我试过了:

    <description>
        <?php
        $content = the_content();
        echo wp_filter_nohtml_kses($content); ?>
    </description>

    <description>
        <?php
        $content = the_content();
        echo strip_tags(html_entity_decode($content)); ?>
    </description>

Feed 中的输出:

<description>
<p><strong>It took Michael Portillo little more than 10 years to get a seat in the Commons and then rise in power and esteem to a point where he was a favoured leader of his party and possible future PM. A track record like that suggests a privileged friend of the rich and famous ,but since leaving the house almost a decade ago Michael has endeared himself to many with his obvious respect for solid workmanship as found in our great Victorian Railways and the daily life of ordinary hard working citizens. Listen to his story, told with a “ parliamentary standup ” wit, and then feel free to question him about it.      </strong></p>
<p><strong>Full Price &#8211; £19.50 </strong></p>
<p><strong>Concession &#8211; £18.50                                                                                                                                                   </strong></p>
        </description>

【问题讨论】:

  • 您可以将 html_entity_decode() 与 strip_tags() 结合使用:echo strip_tags(html_entity_decode($content));

标签: php wordpress rss


【解决方案1】:

使用这个。

wp_filter_nohtml_kses( string $data );

检查此链接。 https://developer.wordpress.org/reference/functions/wp_filter_nohtml_kses/

【讨论】:

  • 谢谢瓜拉夫,不走运。我已经更详细地更新了问题。
  • 你用过wp_strip_all_tags()函数吗??
【解决方案2】:

你可以像这样用 strip_tags 包裹 str_replace:

<?php
function the_content(){
    echo '<p>&nbspContent</p>';
}

$content = the_content();
echo str_replace(' ','',strip_tags($content));
?>

我对此进行了测试,它工作正常。如果这对您不起作用,您能否显示您的功能内容?

编辑:

我还建议您在这里查看 html_entity_decode:

https://www.w3schools.com/php/func_string_html_entity_decode.asp

这是实现您想要的最佳方式。我上面的回答是直接回答您的问题,但这是更好的选择。

【讨论】:

  • 谢谢@Rockhopper。到目前为止没有运气。我在上面添加了更多细节。
  • 嗨,迈克 - 你能也显示 the_content() 的原始内容吗?
  • 啊,对不起,我昏昏欲睡,这是一个 wordpress 网站吗?
  • 是的! Wordpress 网站,使用自定义 RSS 模板
  • 嗯,那可能是问题所在。老实说,我不接触 wordpress,但有一位同事可能会提供帮助。我明天会和他们聊天,然后回来找你。
猜你喜欢
  • 2014-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多