0

カスタム投稿タイプ(と呼ばれる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に制限すると、最新の投稿に添付ファイルがない場合、画像は印刷されません!

カスタム投稿タイプ内の添付ファイル付き投稿の数を制限する方法についての手がかりはありますか?

ありがとう!

4

1 に答える 1

0

ここで、この 2 つの関数に与えられた正しい属性について読んでくださいthe_post_thumbnail('home-parceiros');wp_get_attachment_image( get_the_ID(), 'home-parceiros-foto' );

これが私の提案です:

$query = new WP_Query(
    array(
        'post_type'         => 'parceiros-e-links',
        'posts_per_page'    => 1,
        'orderby'           => 'date',
        'order'             => 'DESC'
    )
);
if($query->have_posts()) :
    while($query->have_posts()) : $query->the_post();
        if(has_post_thumbnail()) : the_post_thumbnail(); endif;
        $args = array(
            'post_type'     => 'attachment',
            'numberposts'   => 1,
            'post_status'   => null,
            'post_parent'   => $post->ID
        );
        $attachments = get_posts( $args );
        if ( $attachments ) {
            foreach ( $attachments as $attachment ) {
                echo wp_get_attachment_image( $attachment->ID, 'full' );
            }
        }
    endwhile;
endif;

私にお知らせください :)

例 #2 が更新されました:

$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' );
        }
    }
}
于 2013-01-29T10:01:54.263 に答える