admin管理员组文章数量:1330394
I'm using the following PHP code to echo post thumbnails.
<?php
$args = array(
'post_type' => 'post' ,
'orderby' => 'date' ,
'order' => 'DESC' ,
'posts_per_page' => 1,
'category' => '2',
'paged' => get_query_var('paged'),
'post_parent' => $parent
);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '' . the_post_thumbnail() . '';
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
?>
But I'm having issue, since this wont work, but when I try to echo get_post_title()
it works perfectly fine.
I can't figure out where do I got this wrong.
How can I display/echo post thumbnails?
I'm using the following PHP code to echo post thumbnails.
<?php
$args = array(
'post_type' => 'post' ,
'orderby' => 'date' ,
'order' => 'DESC' ,
'posts_per_page' => 1,
'category' => '2',
'paged' => get_query_var('paged'),
'post_parent' => $parent
);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '' . the_post_thumbnail() . '';
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
?>
But I'm having issue, since this wont work, but when I try to echo get_post_title()
it works perfectly fine.
I can't figure out where do I got this wrong.
How can I display/echo post thumbnails?
Share Improve this question asked Jul 12, 2020 at 17:27 Yotam DahanYotam Dahan 1096 bronze badges2 Answers
Reset to default 2Not sure what you're trying to do, but the problem you're having with this specific bit of code is that you're using the wrong function. You want get_the_post_thumbnail() instead. The function the_post_thumbnail() echoes the result of get_the_post_thumbnail().
$args = array(
'post_type' => 'post',
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => 1,
'category' => '2',
'paged' => get_query_var('paged'),
'post_parent' => $parent
);
// The Query
$the_query = new WP_Query($args);
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo get_the_post_thumbnail();
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
'posts_per_page' => 1,
Do you really only want a single post to be returned?
And what is the $parent
value?
Since you don't want anything but the post thumbnail, specify that you only need the post id(s). Try:
$args = array(
'post_type' => 'post' ,
'orderby' => 'date' ,
'order' => 'DESC' ,
'posts_per_page' => 1,
'category' => '2',
'paged' => get_query_var('paged'),
'post_parent' => $parent,
'fields' => 'ids'
);
// The Query
$the_query = new WP_Query( $args );
if ( ! empty ( $query->posts ) ) {
$post_ids = $query->posts; // just the post IDs
foreach ( $post_ids as $post_id )
echo get_the_post_thumbnail( $post_id, 'post-thumbnail' );
} else {
echo 'No posts were found';
}
/* Restore original Post Data */
wp_reset_postdata();
A good and relevant read here.
本文标签: theme developmentcannot echo thepostthumbnails
版权声明:本文标题:theme development - cannot echo the_post_thumbnails 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742268438a2443848.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论