外部 Wordpress 博客提要显示

External Wordpress Blog Feed display

正在处理显示外部 wordpress 博客的博客列表。 代码中有一个PHP问题,它只显示一个

  add_shortcode( 'first', 'foobar_func' );

  function foobar_func( $atts ){

    $feed = fetch_feed( 'https://wordpress.org/news/feed/' );

    if (!is_wp_error($feed)):
      $maxitems = $feed->get_item_quantity(3);
      $rss_items = $feed->get_items(CURL_HTTP_VERSION_1_0, $maxitems);
    endif;

    if ($maxitems == 0) {
      return '<li>No items.</li>';
    } else foreach ( $rss_items as $item )  {
      $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $item->get_content(), $matches);
      $first_img = $matches [1][0];
      $testts = esc_html($item->get_title());
    }
    return $testts;
  }

试试这个。注意 CURL_HTTP_VERSION_1_0 已被替换为 0

  add_shortcode( 'first', 'foobar_func' );

  function foobar_func( $atts ){

    $feed = fetch_feed( 'https://wordpress.org/news/feed/' );

    if (!is_wp_error($feed)):
      $maxitems = $feed->get_item_quantity(3);
      $rss_items = $feed->get_items(0, $maxitems);
    endif;

    if ($maxitems == 0) {
      return '<li>No items.</li>';
    } else foreach ( $rss_items as $item )  {
      $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $item->get_content(), $matches);
      $first_img = $matches [1][0];
      $testts = esc_html($item->get_title());
    }
    return $testts;
  }