PHP 数组拼接不起作用



我正在尝试在 wp 中构建一个短代码,以显示来自 2 个 RSS 源的数据。

问题是我无法限制数组中的项目数。我尝试了这些解决方案,但没有一个奏效。

function rss_posts_func( $atts ){
    $feed = fetch_feed(array('rss-feed-1', 'rss-feed-2'));
    $feed = array();
    $feed = array_splice($feed, 0, 3);
    // Loop the results
     $content = '<ul class="rss-aggregator">';
    foreach($feed->get_items() as $item) {
        $content .= '<li class="feed-item">';
        $content .= '<a href='.$item->get_permalink().'>';
        $content .= $item->get_title();
        $content .= '</a></li>';
    }
    $content .= '</ul>';
    return $content;
}

修复:

function rss_posts_func( $atts ){
    $i = 1;
    $feed = fetch_feed(array('rss-feed-1', 'rss-feed-2'));
     $content = '<ul class="rss-aggregator">';
    foreach($feed->get_items() as $item) {
        $content .= '<li class="feed-item">';
        $content .= '<a href='.$item->get_permalink().'>';
        $content .= $item->get_title();
        $content .= '</a></li>';
        if($i++ == 8) break;
    }
    $content .= '</ul>';
    return $content;
}

最新更新