如何截断wp_rss

时间:2016-04-22 14:19:13

标签: php wordpress rss

有没有人知道使用wp_rss截断rss feed的最佳方法?我试着谷歌搜索,似乎找不到任何东西。任何帮助将不胜感激!

这是我的代码:

<?php
include_once(ABSPATH . WPINC . '/rss.php');
$feed = 'http://miletich2.blogspot.co.uk/feed/';
$rss = fetch_feed($feed);
?>
<div class="container">
<div class="right">
<div class="rss-container">
<div class="rss-heading">
<?php
$title = "Clashing With Life";
$description = 'This is a blog written by an autistic person for other autistic people about <br>some of the biggest issues in life, whether deplorable or marvelous.';
echo '<h3 class="text-center">' . $title . '</h3>'; 
echo '<p class="">' . $description . '</p>'; 
?>
</div>
<div class="text-center">
<?php
if (!is_wp_error( $rss ) ) :
        $maxitems = $rss->get_item_quantity(3);
    $rss_items = $rss->get_items(0, $maxitems);
    if ($rss_items):
        echo "<ul>\n";
        foreach ( $rss_items as $item ) : 
            //instead of a bunch of string concatenation or echoes, I prefer the terseness of printf 
            //(http://php.net/manual/en/function.printf.php)

            printf('<div class=""><li><a href="%s">%s</a><p>%s</p><p>%s</p></li></div>',$item->get_permalink(),$item->get_title(),$item->get_date(get_option('date_format')),$item->get_description() );
        '<hr>';
        endforeach;
        echo "</ul>\n";
    endif;
endif;
?>
</div>
</div>
</div>
</div>

1 个答案:

答案 0 :(得分:2)

您可以使用wp_trim_words()wp_strip_all_tags()

示例:

if ( !is_wp_error($rss) ) :
    $maxitems  = $rss->get_item_quantity(3);
    $rss_items = $rss->get_items(0, $maxitems);
    if ($rss_items):
        foreach ($rss_items as $item) :
            $item_title     = esc_attr( $item->get_title() );
            $item_permalink = esc_url( $item->get_permalink() );
            $item_post_date = $item->get_date( get_option('date_format') );
            $item_excerpt   = wp_trim_words( wp_strip_all_tags( $item->get_description(), true ), 55 );
            echo sprintf('<div class=""><a href="%s">%s</a><p>%s</p><p>%s</p></div>', $item_permalink, $item_title, $item_post_date, $item_excerpt);
        endforeach;
    endif;
endif;

顺便说一下,include_once(ABSPATH . WPINC . '/rss.php');是不必要的。你应该删除它。