カスタム投稿タイプ(と呼ばれるparceiros-e-links
)の最後の投稿から、投稿のサムネイルと別の添付画像を取得しようとしています。
私が得たのは、画像付きのすべての投稿を取得して表示することです...しかし、最後の投稿のみを表示し、2つの画像(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に制限すると、最新の投稿に添付ファイルがない場合、画像は印刷されません!
カスタム投稿タイプ内の添付ファイル付き投稿の数を制限する方法についての手がかりはありますか?
ありがとう!