我使用以下PHP代码来回显post缩略图。
'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();
?>但我有问题,因为这是行不通的,但当我试图回显get_post_title()时,它的工作非常好。我不知道我在哪里搞错了。
如何显示/回波后缩略图?
发布于 2020-07-13 00:25:29
不确定您想要做什么,但是这段特定代码的问题是您使用了错误的函数。您需要的是get_the_post_thumbnail()。函数the_post_thumbnail()回显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();发布于 2020-07-12 18:32:25
'posts_per_page' => 1,你真的只想要回一个帖子吗?$parent值是多少?
由于您只需要post缩略图,所以指定您只需要post id(s)。尝试:
$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();一个好的,相关的阅读这里。
https://wordpress.stackexchange.com/questions/370916
复制相似问题