帖子图库列表附件,用作帖子缩略图的附件除外

时间:2012-03-04 作者:svassr

我在一篇文章中做了图库后的文章缩略图。因此,我列出了所有带有图像类型的帖子附件(使用以下查询),但我想排除帖子缩略图。

global $post;
$args = array(
    \'post_type\' => \'attachment\',
    \'post_mime_type\' => \'image\',
    \'numberposts\' => -1,
    \'orderby\' => \'menu_order\',
    \'order\' => \'ASC\',
    \'post_parent\' => $post->ID
);
$images = get_posts($args);
我的第一个想法是将缩略图的全尺寸url与每个附件src进行比较。

$large_image_url = "";
if ( has_post_thumbnail() ):
    $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), \'full\');
    $large_image_url = parse_url($large_image_url[0], PHP_URL_PATH);
/* 
 *  [...] 
 */
global $post;
$args = array(
    \'post_type\' => \'attachment\',
    \'post_mime_type\' => \'image\',
    \'numberposts\' => -1,
    \'orderby\' => \'menu_order\',
    \'order\' => \'ASC\',
    \'post_parent\' => $post->ID
);
$images = get_posts($args);
foreach($images as $image){
    $big_array = image_downsize( $image->ID, \'full\' );
    $img_url = $big_array[0];
    if(!empty($large_image_url) && $large_image_url == $img_url){
    continue;
    }
    array_push($gallery, array(
            \'dirname\' => dirname($img_url),
            \'filepath\' => parse_url($img_url, PHP_URL_PATH),
            \'ID\' => $image->ID
        ));
}
但实际上它还没有起作用。有人知道更简单或更整洁的方法吗?谢谢

1 个回复
最合适的回答,由SO网友:Rob Vermeer 整理而成

您可以使用exclude 参数

global $post;
$args = array(
    \'exclude\' => get_post_thumbnail_id( $post->ID )
    \'post_type\' => \'attachment\',
    \'post_mime_type\' => \'image\',
    \'numberposts\' => -1,
    \'orderby\' => \'menu_order\',
    \'order\' => \'ASC\',
    \'post_parent\' => $post->ID
);
$images = get_posts($args);
http://codex.wordpress.org/Template_Tags/get_posts
http://codex.wordpress.org/Function_Reference/get_post_thumbnail_id

结束

相关推荐

Pull images from the gallery

我正在构建一个模板,wordpress主要用作图像CMS,我从每篇文章的内容部分提取第一幅图像。<img src=\"<?php echo grab_image() ?>\" /> function grab_image() { global $post, $posts; $first_img = \'\'; ob_start(); ob_end_clean(); $output = preg_match_all(\'/&l