0

I'm doing gallery in a single post after the post thumbnail. So I list all the post attachment with the image type (with the following query) but I want to exclude the post thumbnail.

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);

My first idea was to compare the thumbnail full size url with each attachment 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
        ));
}

But actually it doesn't work yet. Does someone know a simpler or neater way to do so ? Thanks

  • Welcome to WPSE. »But actually it doesn't work yet.« isn't quite helpful. Maybe you want to add some more details? Adding some comments to your code would help to see your intentions and seeing your error messages (WP_DEBUG to true in the wp-config.php) also helps. Btw: Your if( has_post_thumbnail() ) : has no endif; - where should that be? – kaiser Mar 5 '12 at 0:05
2

You can use the exclude parameter.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.