Php WordPress循环中的“发布”缩略图不起作用
我也是新来的,我不能让我的循环显示WordPress帖子中的特色图片 我已经试过使用“后”缩略图和 仔细研究了其他类似的问题 希望你能帮忙 我的循环现在看起来像这样:Php WordPress循环中的“发布”缩略图不起作用,php,wordpress,loops,templatetags,Php,Wordpress,Loops,Templatetags,我也是新来的,我不能让我的循环显示WordPress帖子中的特色图片 我已经试过使用“后”缩略图和 仔细研究了其他类似的问题 希望你能帮忙 我的循环现在看起来像这样: <?php $query = new WP_Query(array( 'posts_per_page' => 4, )); while ($query->have_posts()): $query->the_post(); ?> <ul>
<?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();
?>
-
我在这里为您发送代码,请检查
<?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
}
- “/>
你可以试试这个代码,也许它能用
<?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();
?>
-
您需要将添加主题支持('post thumbnails');
添加到您的函数中。php
文件才能使缩略图正常工作我已经修复了代码。在这种情况下,您可以使用获取\u post\u缩略图('post\u id)
函数并将其分配给一个变量。然后,您可以回显它
<?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();
?>
-
您可以在查看这两个函数之间的详细差异,它是否正确显示了标题和日期?嗨,Sofie,您能确认这个循环是用于自定义帖子类型还是默认WordPress帖子吗?多亏了@Jagir bahesh,我已经解决了这个问题。(见最后一个答案)确保您已在post/page.Hi中添加了特色图像。谢谢,这是我丢失的“特色图像”。我刚刚在post文本中添加了图像,而不是特色图像框。不应该$theu query=new WP\u query($args);
是$theu query=new WP\u query($myquery)
建议的编辑队列已满,所以我在这里做一个说明:您自相矛盾,$thumbnail=发布缩略图(…)
不是正确的用法。应该是$thumbnail=获取发布缩略图(…)