[英]the_post_thumbnail(); not working when calling post based on category in WordPress
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'order' => 'DESC' ,
'cat' => '3',
);
$arr_posts = new WP_Query( $args );
if ( $arr_posts->have_posts() ) :
while ( $arr_posts->have_posts() ) :
$arr_posts->the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<a class="post-thumbnail" style="width: 250px;height: 147px;">
<?php
$attimages = get_attached_media('image', $post->ID);
foreach ($attimages as $image) {
?>
<img src="<?php echo wp_get_attachment_url($image->ID);?>" > <?php
}
?> </a>
<div class="entry-header">
<h3 class="entry-title"><a target="_self" href=<?php the_permalink(); ?>><?php the_title(); ?></a></h3></br>
<div class="entry-content">
<?php the_excerpt(); ?>
<a href="<?php the_permalink();
?>">Read More</a>
</div>
</div>
</li>
</br></ul>
</article>
<?php
endwhile;
I am trying to display all post from category 3 to a particular page.我正在尝试将第 3 类中的所有帖子显示到特定页面。 I am getting the title and excerpt correctly.But I am not getting the image correctly.
我正确地获得了标题和摘录。但我没有正确地获得图像。
I first Used :我第一次使用:
the_post_thumbnail();
But it didnt worked.但它没有奏效。
Then I used然后我用
$attimages = get_attached_media('image', $post->ID);
Then For I post,I got image so I printed $attimages and found empty array.然后对于我发布,我得到了图像,所以我打印了 $attimages 并找到了空数组。
Any help任何帮助
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'order' => 'DESC' ,
'cat' => '3',
);
$arr_posts = new WP_Query( $args );
if ( $arr_posts->have_posts() ) :
while ( $arr_posts->have_posts() ) :
$arr_posts->the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<a class="post-thumbnail" style="width: 250px;height: 147px;">
<?php $attimages = get_attached_media('image', $post->ID);
foreach ($attimages as $image) { ?>
<img src="<?php echo wp_get_attachment_url($image->ID);?>" >
<?php } ?>
</a>
<div class="entry-header">
<h3 class="entry-title"><a target="_self" href=<?php the_permalink(); ?>><?php the_title(); ?></a></h3>
<div class="entry-content">
<?php the_excerpt(); ?>
<a href="<?php the_permalink();?>">Read More</a>
</div>
</div>
</article>
<?php
endwhile;
endif;
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.