当使用“orderby=comment_count”时帖子缩略图消失(WordPress)
我正在尝试根据侧边栏中的评论显示最受欢迎的帖子。我希望帖子缩略图显示在标题旁边,但是当我使用“orderby=comment_count”对帖子进行排序时,缩略图会消失。如果我根据类别名称显示帖子,则会显示缩略图。作为参考,这是我的代码:
<?php $post_by_comment = new WP_Query('orderby=comment_count&posts_per_page=6'); ?>
<?php while ($post_by_comment->have_posts() ) : $post_by_comment->the_post(); ?>
<?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail();
}
?>
<?php endwhile;?>
如果我使用完全相同的代码但更改:
到
帖子缩略图显示得很好。我在这里做错了什么?
I am trying to display the most popular posts based on comments in my sidebar. I want the post thumbnail to show up next to the title but when I sort my posts using 'orderby=comment_count' the thumbnail disappears. If I show the posts based on the category name the thumbnail shows up. For reference, here is my code:
<?php $post_by_comment = new WP_Query('orderby=comment_count&posts_per_page=6'); ?>
<?php while ($post_by_comment->have_posts() ) : $post_by_comment->the_post(); ?>
<?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail();
}
?>
<?php endwhile;?>
If I use the exact same code but change:
<?php $post_by_comment = new WP_Query('orderby=comment_count&posts_per_page=6'); ?>
to
<?php $post_by_comment = new WP_Query('category_name=categoryname&posts_per_page=6'); ?>
the post thumbnails show up just fine. What am I doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试 get_the_post_thumbnail 并将您要查找的 ID 传递给它。
我认为为什么会出错,可能是 the_post_thumbnail 期望 ID 是 $post->ID 而不是 $post_by_comment->ID (就像你一样)。
如果这只是侧面列表而不是页面的主要内容,我还会使用 get_posts 而不是开始新查询。
Try get_the_post_thumbnail and pass it the ID you are looking for.
I think why this is going wrong is perhaps that the_post_thumbnail expects the ID to be $post->ID rather than $post_by_comment->ID (as you have).
I'd also use get_posts rather than starting a new query if this is just a side listing, rather than the main content of the page.