douwei3280 2015-06-23 11:12 采纳率: 0%
浏览 65
已采纳

从另一个WordPress网站拉取帖子

I am trying to get the 2 latest posts from my personal website, using the code below from http://codex.wordpress.org/Function_Reference/fetch_feed#Usage

<h2><?php _e( 'Recent news from Some-Other Blog:', 'my-text-domain' ); ?></h2>

<?php // Get RSS Feed(s)
include_once( ABSPATH . WPINC . '/feed.php' );

// Get a SimplePie feed object from the specified feed source.
$rss = fetch_feed( 'THISISWHEREMYURLGOES/' );

$maxitems = 0;

if ( ! is_wp_error( $rss ) ) : // Checks that the object is created correctly

    // Figure out how many total items there are, but limit it to 5. 
    $maxitems = $rss->get_item_quantity( 2 ); 

    // Build an array of all the items, starting with element 0 (first element).
    $rss_items = $rss->get_items( 0, $maxitems );

endif;
?>

<ul>
<?php if ( $maxitems == 0 ) : ?>
    <li><?php _e( 'No items', 'my-text-domain' ); ?></li>
<?php else : ?>
    <?php // Loop through each feed item and display each item as a hyperlink. ?>
    <?php foreach ( $rss_items as $item ) : ?>
        <?php echo esc_html( $item->get_title() );  ?>
        <li>
            <a href="<?php echo esc_url( $item->get_permalink() ); ?>"
                title="<?php printf( __( 'Posted %s', 'my-text-domain' ), $item->get_date('j F Y | g:i a') ); ?>">
                <?php echo esc_html( $item->get_title() ); ?>                    
                <?php printf( __( 'Posted %s', 'my-text-domain' ), $item->get_date('j F Y | g:i a') ); ?>

            </a>
        </li>
    <?php endforeach; ?>
<?php endif; ?>

With this code, I can get the Posts URL, the title and the date posted, which is great!

Now, trying to get the image is another issue. I am trying to use :

<?php echo esc_html( $item->the_post_thumbnail() ); ?> 

But I get the error : Fatal error: Call to undefined method SimplePie_Item::the_post_thumbnail()

So, using SimplePie, is there a way to get the posts image?


MAJOR EDIT:

This way of getting the RSS feed isn't great, it is causing alot of issues throughout the site, so if anyone could show me/direct me to something where I can get the 4 latest posts from another WordPress site, that'd be awesome!

  • 写回答

2条回答 默认 最新

  • donglue1886 2015-06-25 12:28
    关注

    As you've found, WordPress feeds have some limitations. Since you've asked for an alternative solution, I'd definitely recommend using WP REST API.

    Since WP API isn't yet part of the WP Core, you'll want to do the following:

    1. Head to your Plugins panel (on the site you're trying to pull posts from...your personal website) and install WP REST API (WP API).
    2. Activate the plugin
    3. Getting your posts is as easy as going to: http://yoursite.com/wp-json/posts

    Since you only want four posts, you can use filters:

    http://yoursite.com/wp-json/posts?filter[posts_per_page]=4
    

    To get this JSON into a usable state in PHP:

    // Get the JSON
    $json = file_get_contents('http://yoursite.com/wp-json/posts?filter[posts_per_page]=4');
    // Convert the JSON to an array of posts
    $posts = json_decode($json);
    

    You can now digest this $posts array however you want (by looping through it). For example:

    foreach ($posts as $p) {
        echo '<p>Title: ' . $p->title . '</p>';
        echo '<p>Date:  ' . date('F jS', strtotime($p->date)) . '</p>';
        // Output the featured image (if there is one)
        echo $p->featured_image ? '<img src="' . $p->featured_image->guid . '">' : '';
    }
    

    More info in the WP API docs.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程