限制Wordpress自定义帖子类型查询中附件的帖子

时间:2013-01-29 06:54:27

标签: php wordpress attachment custom-post-type

我正在尝试从自定义帖子类型的最后一个帖子(称为parceiros-e-links)获取帖子缩略图和其他附加图片。

我得到的是获取带有图片的所有帖子并显示它们......但我只需要显示最后一篇文章并用两张图片显示它(the_post_thumbnailwp_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,如果最近的帖子没有附件,不会打印图像!

有关如何限制自定义帖子类型中附件的帖子数量的任何线索?

谢谢!

1 个答案:

答案 0 :(得分:0)

您的错误发生在the_post_thumbnail('home-parceiros');和echo 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' );
        }
    }
}