【问题标题】:Limit posts with attachment inside Wordpress custom post type query在 Wordpress 自定义帖子类型查询中限制带有附件的帖子
【发布时间】:2013-01-29 06:54:27
【问题描述】:

我正在尝试从自定义帖子类型(称为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,如果最近的帖子没有附件,不会打印任何图像!

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

谢谢!

【问题讨论】:

  • 你检查过codex.wordpress.org/Function_Reference/is_singular 吗?这将帮助您检查多个帖子类型。
  • 我直到现在才知道 is_singular @Rikesh 谢谢!我也在谷歌上搜索过它,但我不知道它对我有什么帮助。显示一篇文章时会中断查询吗?
  • 不,它不会破坏你的循环,但你可以用它来检查条件,只有当你的 post_type 是 parceiros-e-links && attachment 时才会返回 true,否则它会返回 false。跨度>

标签: php wordpress attachment custom-post-type


【解决方案1】:

您的错误在于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' );
        }
    }
}

【讨论】:

  • 此代码打印了两次相同的图像;我想在同一篇文章的图片旁边打印 the_post_thumbnail,但不是缩略图!实际上我用我的代码得到了它,但它打印了所有带有图像的帖子,我只需要最后一个。我在the_post_thumbnail('home-parceiros');wp_get_attachment_image( get_the_ID(), 'home-parceiros-foto' ); 上找不到错误(我在functions.php 中使用自定义图像格式);不过谢谢!
  • 最后一个帖子?更新了我的答案并添加了示例 #2,区别在于 posts_per_page 值:)
  • 问题是我需要始终显示图像(我需要最后一个带有图像的帖子),因此如果最后一个帖子没有图像,则使用此代码将不显示:(
  • 如果您需要始终显示图片,我认为它需要始终将图片附加到帖子中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多