1

For my jigoshop theme I'm able to retrieve the images attached to my product post. However it pulls all images from both the featured image and my metabox images. What I'm trying to do is make it so that it excludes the featured image, but it's not working. It also seems to be including other post featured images as well for some reason.

I tried this suggestion with no luck.

Here's what I have so far:

    global $_product, $post; 
    $thumb_id = get_post_thumbnail_id($post->ID);

    $args = array(
    'post_type' => 'attachment',
    'post_mime_type' => 'image',
    'numberposts' => -1,
    'post_status' => null,
    'post_parent' => $post->ID,
    'orderby' => 'menu_order',
    'exclude' => $thumb_id,
    'order' => 'asc'
    );

    $attachments = get_posts($args);
    if ($attachments) :
        $loop = 0;

        foreach ( $attachments as $attachment ) :

            if ($thumb_id==$attachment->ID) continue;

            $loop++;

            $_post =  get_post( $attachment->ID );
            $url = wp_get_attachment_url($_post->ID);
            $post_title = esc_attr($_post->post_title);
            $image = wp_get_attachment_image_src($attachment->ID, 'full');
            $image = $image[0];
            $image_path =  thumbGen($image,500,0,"crop=1&halign=center&valign=center&return=1");
            $image_all = get_bloginfo('url').$image_path;
            $my_image = array_values(getimagesize($image_all));
            list($width, $height, $type, $attr) = $my_image;            

            if ( ! $image || $url == get_post_meta($post->ID, 'file_path', true) )
                continue;

            echo '<span><img src="'.$image.'" alt="'.get_the_title().'" title="'.get_the_title().'" width="'.$width.'" height="'.$height.'"/></span>';

        endforeach;

    endif;

Any ideas?

1

Try to use that set of arguments :

$args = array(
    'post_type' => 'attachment',
    'post_mime_type' => 'image',
    'numberposts' => -1,
    'post_parent' => $post->ID,
    'orderby' => 'menu_order',
    'order' => 'asc',
    'post__not_in' => array( $thumb_id )
);

I'm using it for one of my site and it's working fine. So you shouldn't have to use :

 if ($thumb_id==$attachment->ID) continue;

in your loop.

Strangely, the get_posts() Codex Page (http://codex.wordpress.org/Template_Tags/get_posts) says that we could use a exclude parameter, but also says that get_posts() use WP_Query(). And there is no exclude parameter in the Codex for WP_Query()(http://codex.wordpress.org/Class_Reference/WP_Query), only post__not_in.

So, let's use post__not_in instead of exclude.

Looks like a discrepancy.

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.