Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
    
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
add comment

1 Answer 1

up vote 2 down vote accepted

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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