WP_GET_ATTACHING_IMAGE不获取POST_THMBNAILL

时间:2016-03-06 作者:Gas

我有一个设置,在那里我想实现我有一个产品的大图(张贴图像),然后下面是其他缩略图列表,可以在灯箱中打开。我这样做是通过以适当的大尺寸查询帖子图像,然后通过WP\\u Query post类型查询附件图像。但是,问题是,查询也会得到帖子缩略图,而不仅仅是贴在帖子上的图像。这是代码

<div class="product-img">

    <?php if ( has_post_thumbnail()) : // Check if Thumbnail exists ?>
    <?php // get fullimage
    $thumb_id = get_post_thumbnail_id( $post->ID ); 
    $image = wp_get_attachment_image_src( $thumb_id,\'full\' ); ?>

        <a href="<?php echo $image[0]; ?>" title="<?php the_title(); ?>" rel="lightbox">
            <?php the_post_thumbnail( \'large\' ); // large image for the single post ?>
        </a>
    <?php endif; ?>

    <div class="other-images">
    <?php /* QUery attached images */
    $images_query_args = array(
        \'post_type\' => \'attachment\',
        \'post_status\' => \'inherit\',
        \'post_mime_type\' => \'image\',
        \'post_parent\' => $post->ID
    );

    $images_query = new WP_Query( $images_query_args );

    if ( $images_query->have_posts() ) : while ( $images_query->have_posts() ) : $images_query->the_post();
    ?>

    <a href="<?php echo wp_get_attachment_image_url( get_the_ID(), \'large\' ); ?>" title="<?php the_title(); ?>" rel="lightbox">
        <?php   // Output the attachment image
        echo wp_get_attachment_image( get_the_ID(), \'thumbnail\' ); ?>
    </a>

    <?php
    endwhile; endif;
    wp_reset_postdata();
    ?>

</div>
如何使image\\u查询不显示post图像,而只显示其他图像?或者,如果有一个插件可以比原生WP更好地处理附加的库(例如自定义排序顺序,更好地可视化附加的图像),我也有兴趣在必要时重新编写代码。对于编辑来说,重要的是这些图库是按帖子排列的,而不是通过诸如NextCellent这样的短代码分开的

2 个回复
最合适的回答,由SO网友:Milo 整理而成

您可以使用post__not_in 要按ID排除图像,请执行以下操作:

$images_query_args = array(
    \'post_type\' => \'attachment\',
    \'post_status\' => \'inherit\',
    \'post_mime_type\' => \'image\',
    \'post_parent\' => $post->ID,
    \'post__not_in\' => array(get_post_thumbnail_id())
);

SO网友:Gas

这是正确的代码,如上所示https://codex.wordpress.org/Function_Reference/get_post_thumbnail_id#Show_all_attachments_for_the_current_post_except_the_Featured_Image

<?php

$args = array(
    \'post_type\'   => \'attachment\',
    \'numberposts\' => -1,
    \'post_status\' => \'any\',
    \'post_parent\' => $post->ID,
    \'exclude\'     => get_post_thumbnail_id(),
);

$attachments = get_posts( $args );

if ( $attachments ) {
    foreach ( $attachments as $attachment ) {
        echo apply_filters( \'the_title\', $attachment->post_title );
        the_attachment_link( $attachment->ID, false );
    }
}

?>

相关推荐

如何使用Get Media Attachments遍历所有帖子和计算附件

我正在编写一个代码,在这个代码中,我需要循环浏览给定作者的所有帖子,然后针对该作者的每篇帖子,计算该帖子的媒体附件数量,然后对其进行回应。我能够循环浏览帖子,效果如预期,但我想计算每个帖子的附件数量,如果我失败了,请帮助。$author_posts = get_posts( array(\'author\' => $author_id, \'numberposts\' => -1, \'post_type\'=> array(\'post\',\'download\', \'attach