问题描述
我正在尝试使用以下 http中的代码从我的个人网站上获得2条最新帖子. ://codex.wordpress.org/Function_Reference/fetch_feed#用法
<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; ?>
使用此代码,我可以获取帖子URL,标题和发布日期,这太好了!
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() ); ?>
但是我得到了错误:致命错误:调用未定义的方法SimplePie_Item :: the_post_thumbnail()
But I get the error :Fatal error: Call to undefined method SimplePie_Item::the_post_thumbnail()
因此,使用SimplePie,有没有办法获取帖子图像?
So, using SimplePie, is there a way to get the posts image?
主要
这种获取RSS feed的方法不好,它在整个网站上引起了很多问题,因此,如果有人可以向我展示/将我定向到可以从另一个WordPress网站获取4个最新帖子的地方,那太棒了!
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!
推荐答案
如您所见,WordPress提要有一些限制.由于您已寻求替代解决方案,因此,我绝对建议您使用 WP REST API .
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.
由于WP API尚未成为WP Core的一部分,因此您需要执行以下操作:
Since WP API isn't yet part of the WP Core, you'll want to do the following:
- 转到插件"面板(正在尝试从您的个人网站上提取帖子的网站上)并安装 WP REST API(WP API).
- 激活插件
- 获取帖子与访问以下内容一样容易:
http://yoursite.com/wp-json/posts
- 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).
- Activate the plugin
- 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
要使此JSON在PHP中变为可用状态:
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);
现在,您可以根据需要消化此$posts
数组(通过遍历它).例如:
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 . '">' : '';
}
更多信息在WP API文档中.
这篇关于从另一个WordPress网站提取帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!