我需要在WordPress网站外部创建最近发布的帖子部分,但结果不正确(请参阅图片)。我使用了这个PHP代码:
// elsewhere in code...
require "wp-load.php";
// the code that is generating the recent posts section
$posts = get_posts( array( 'posts_per_page' => 1 ) );
$recent_posts = wp_get_recent_posts(array('numberposts' => 7 ) );
foreach ($posts as $_post) {
foreach($recent_posts as $post) {
if ( has_post_thumbnail( $_post->ID ) ) {
echo '<div class="owl-item">';
echo '<figure class="blog-item-container">';
echo '<span class="blog-item-img">';
echo get_the_post_thumbnail( $_post->ID, array(480, 305) );
echo '</span>';
echo '<figcaption>';
echo '<h3><a href="', get_permalink($post['ID']), '">', $post['post_title'], '</a></h3>';
echo '</figcaption>';
echo '</figure>';
echo '</div>';
}
}
}
我希望得到最近每篇特定帖子的图片。这个结果给了我正确的最近帖子,但所有帖子的图像都相同。为什么图像一样?
答案 0 :(得分:0)
以下是对您的代码的一些评论,希望能够解释出现了什么问题,或者帮助您了解它的底部:
// Here you are getting ONE post for some reason, and putting it in the $posts variable
$posts = get_posts( array( 'posts_per_page' => 1 ) );
// Here you are getting 7 posts, and putting in the $recent_posts variable
$recent_posts = wp_get_recent_posts(array('numberposts' => 7 ) );
// Here you are "looping" over the ONE post and putting into $_post variable.
// So here, $_post is made to reference the single most recent post
foreach ($posts as $_post) {
// Here, you are "looping" over the 7 recent posts, putting into $post variable
foreach($recent_posts as $post) {
// Here you ask if the ONE most recent post ($_post) has a featured image.
// I doubt this is what you actually want, as this will
// be run for all 7 recent posts, but is only referencing
// the single ONE post
if ( has_post_thumbnail( $_post->ID ) ) {
echo '<div class="owl-item">';
echo '<figure class="blog-item-container">';
echo '<span class="blog-item-img">';
// here you are displaying the image for the ONE post, not the 7 most recent posts
echo get_the_post_thumbnail( $_post->ID, array(480, 305) );
echo '</span>';
echo '<figcaption>';
// Here you are referencing the title, link, etc.
// for the 7 most recent posts
echo '<h3><a href="', get_permalink($post['ID']), '">', $post['post_title'], '</a></h3>';
echo '</figcaption>';
echo '</figure>';
echo '</div>';
}
}
}
因此,考虑到您最近发布的帖子&#34;以及您的评论不正确以显示所有相同的精选图片,我怀疑代码应该看起来像这样(注意:你可以删除所有注释的代码,我把它留在这里解释变化是什么):
// remove this - no need for the single most recent post
// $posts = get_posts( array( 'posts_per_page' => 1 ) );
$recent_posts = wp_get_recent_posts(array('numberposts' => 7 ) );
// remove this - don't loop over the single most recent post
// foreach ($posts as $_post) {
foreach($recent_posts as $post) {
// change this to $post['ID'] (from $_post->ID)
if ( has_post_thumbnail( $post['ID'] ) ) {
echo '<div class="owl-item">';
echo '<figure class="blog-item-container">';
echo '<span class="blog-item-img">';
// change this to $post['ID'] (from $_post->ID)
echo get_the_post_thumbnail( $post['ID'], array(480, 305) );
echo '</span>';
echo '<figcaption>';
echo '<h3><a href="', get_permalink($post['ID']), '">', $post['post_title'], '</a></h3>';
echo '</figcaption>';
echo '</figure>';
echo '</div>';
}
}
// remove this, since we are removing the loop
// }