我正在尝试从自定义帖子类型(称为parceiros-e-links
)的最后一个帖子中获取帖子缩略图和另一个附加图像。
我得到的是获取所有带有图像的帖子并显示它们......但我只需要显示最后一个帖子并用两个图像(the_post_thumbnail
和wp_get_attachment_image
)显示它
这是我当前的代码:
<?php
$query = new WP_Query( array( 'post_type' => 'parceiros-e-links', 'posts_per_page' => -1, 'orderby' => 'date', 'order' => 'DESC' ) ); //the first loop where I filter the posts from custom post type and by date
if( $query->have_posts() ){
while($query->have_posts()){
$query->the_post();
$image_query = new WP_Query( array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'post_mime_type' => 'image', 'posts_per_page' => 2, 'post_parent' => get_the_ID() ) ); //the second loop where I filter the posts with attachments and limit to two
while( $image_query->have_posts() ) { $image_query->the_post();
//code below prints the two attachments: thumbnail and another one.
if(has_post_thumbnail()){
the_post_thumbnail('home-parceiros');
}
echo wp_get_attachment_image( get_the_ID(), 'home-parceiros-foto' );
}
}
}
?>
我已经花了很多时间搜索类似的情况并尝试过滤此代码,但我没有想法...因为如果我将查询的第一个限制posts_per_page
为 1,如果最近的帖子没有附件,则不会打印任何图像!
关于如何限制自定义帖子类型中带有附件的帖子数量的任何线索?
谢谢!