[英]the_post_thumbnail in WordPress Loop wont work
I'm new too this, and I can't get my loop to show the featured image from posts in WordPress. 我也是这个新手,我无法循环显示WordPress帖子中的特色图片。
I have tried with the_post_thumbnail and looked through https://codex.wordpress.org/Post_Thumbnails and other similar questions. 我尝试过the_post_thumbnail,并通过https://codex.wordpress.org/Post_Thumbnails和其他类似问题进行了研究。
Hope you can help. 希望能对您有所帮助。
My loop looks like this now: 我的循环现在看起来像这样:
<?php
$query = new WP_Query(array(
'posts_per_page' => 4,
));
while ($query->have_posts()): $query->the_post(); ?>
<ul>
<li><?php the_title(); ?></li>
<li><?php echo get_the_date(); ?></li>
<li><?php
if ( has_post_thumbnail() ) {
the_post_thumbnail('thumbnail');
} ?>
</li>
</ul>
<?php endwhile;
wp_reset_postdata();
?>
Here I am sending the code for you please check. 在这里,我正在为您发送代码,请检查。
<?php
//Args
$myquery = array(
'post_type' => 'post', // Here you add your post type
'posts_per_page' => 4,
'orderby' => 'post_date',
'order' => 'DESC'
);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post(); ?>
<?php /* grab the url for the full size featured image */
$featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'full'); ?>
<ul>
<li><?php echo get_the_title(); ?></li>
<li><?php echo get_the_date(); ?></li>
<li><img src="<?php echo $featured_img_url; ?>" /></li>
</ul>
<?php }
/* Restore original Post Data */
wp_reset_postdata();
} else {
// no posts found
}
You can try this code, maybe it works. 您可以尝试此代码,也许可以。
<?php
$query = new WP_Query(array(
'posts_per_page' => 4,
));
while ($query->have_posts()): $query->the_post(); ?>
<ul>
<li><?php the_title(); ?></li>
<li><?php echo get_the_date(); ?></li>
<li><?php
if ( has_post_thumbnail() ) {
$image = get_the_post_thumbnail('thumbnail');
} ?>
</li>
</ul>
<?php endwhile;
wp_reset_postdata();
?>
You need to add add_theme_support( 'post-thumbnails' );
您需要添加
add_theme_support( 'post-thumbnails' );
to your functions.php
file in order for thumbnails to work 到您的
functions.php
文件,以便缩略图可以工作
I have fixed the code. 我已经修复了代码。 In such cases, you can use
get_the_post_thumbnail( $post_id )
function and assign it to a variable. 在这种情况下,可以使用
get_the_post_thumbnail( $post_id )
函数并将其分配给变量。 Then, you can echo
it. 然后,您可以
echo
它。
<?php
$query = new WP_Query(array(
'posts_per_page' => 4,
));
while ($query->have_posts()): $query->the_post(); ?>
<ul>
<li><?php the_title(); ?></li>
<li><?php echo get_the_date(); ?></li>
<li><?php
$thumbnail = '';
if ( has_post_thumbnail( get_the_ID() ) ) {
$thumbnail = the_post_thumbnail( get_the_ID(), 'thumbnail');
}
echo $thumbnail; ?>
</li>
</ul>
<?php endwhile;
wp_reset_postdata();
?>
You can go through the detailed difference between the 2 functions at How to get featured image in WordPress - the_post_thumbnail & get_the_post_thumbnail 您可以在如何在WordPress中获取特色图片中详细了解这两个功能之间的区别-the_post_thumbnail&get_the_post_thumbnail
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.