0

I'm trying to create a gallery page that takes image attachments from a certain category and displays them. It works but I have a problem it seems where I'm trying to exclude certain attachment types from the returned attachment images.

I am also using the WCK plugin to create some custom fields for the blog posts for images of differing sizes to be displayed in different locations. On the home page there is a more square one, on the blog page there is a skinny one, and on the actual post page there is a slightly bigger skinny version of the image. Here is how I'm calling the attachments, and there you will see how I am trying to exclude those images.

// WP_Query arguments
    $args = array (
        'post_type'               => 'post',
        'pagination'             => false,
        'posts_per_page'     => '99',
        'category_name' => 'family'
    );

    // The Query
    $query = new WP_Query( $args );

    // The Loop
    if ( $query->have_posts() ) {
        while ( $query->have_posts() ) {
            $query->the_post();

            $thumb_ID = get_post_thumbnail_id( $post->ID );
            $blogimages = get_post_meta( get_the_ID(), 'blogimages', true ); 
            if ( $images = get_children(array(
                    'post_parent' => $post->ID,
                    'post_type' => 'attachment',
                    'post_mime_type' => 'image',
                    'exclude' => [$thumb_ID,$blogimages[0]['blog-image-inside'],$blogimages[0]['blog-image-front'],$blogimages[0]['blog-image-main']],
                ))) : 
                foreach( $images as $image ) : 
                    echo '<div class="item">';
                    echo '<a href="'.wp_get_attachment_url($image->ID).'" rel="nivo">';
                    echo wp_get_attachment_image($image->ID, 'full');
                    echo '</a>';
                    echo '</div>';
                    endforeach;
        else: // No images
            //This post has no attached images
        endif;
        }
    }
    // Restore original Post Data
    wp_reset_postdata();

I believe the problem is that those images are not set on those blog posts, so It is having an issue, perhaps because they are set as undefined? How can I fix this so that if the image is not uploaded it will exclude it and if it is uploaded it will still be excluded.

The errors I'm getting by the way look like this over and over I assume for as many times as it's grabed a blog post and attempted to grab it's attachments

Notice: Uninitialized string offset: 0 in /Applications/MAMP/htdocs/karmen-noel/wp-content/themes/karmen_noel/page-galleries-family.php on line 50

Warning: Illegal string offset 'blog-image-inside' in /Applications/MAMP/htdocs/karmen-noel/wp-content/themes/karmen_noel/page-galleries-family.php on line 50

Notice: Uninitialized string offset: 0 in /Applications/MAMP/htdocs/karmen-noel/wp-content/themes/karmen_noel/page-galleries-family.php on line 50

Notice: Uninitialized string offset: 0 in /Applications/MAMP/htdocs/karmen-noel/wp-content/themes/karmen_noel/page-galleries-family.php on line 50

Warning: Illegal string offset 'blog-image-front' in /Applications/MAMP/htdocs/karmen-noel/wp-content/themes/karmen_noel/page-galleries-family.php on line 50

Notice: Uninitialized string offset: 0 in /Applications/MAMP/htdocs/karmen-noel/wp-content/themes/karmen_noel/page-galleries-family.php on line 50

Notice: Uninitialized string offset: 0 in /Applications/MAMP/htdocs/karmen-noel/wp-content/themes/karmen_noel/page-galleries-family.php on line 50

Warning: Illegal string offset 'blog-image-main' in /Applications/MAMP/htdocs/karmen-noel/wp-content/themes/karmen_noel/page-galleries-family.php on line 50

Notice: Uninitialized string offset: 0 in /Applications/MAMP/htdocs/karmen-noel/wp-content/themes/karmen_noel/page-galleries-family.php on line 50
  • Which is line 50 in the code above? And what does this mean "if the image is not uploaded it will exclude it and if it is uploaded it will still be excluded"? How do you exclude an image that has not been uploaded? – s_ha_dum May 17 '14 at 13:39
  • Line 50 is the exclude line, sorry. That's exactly what I'm trying to work with, I essentially want it to exclude that image if it is set, but if not then don't worry about it, can I just wrap in an isset? I'm not sure I can do that in an array though. – Jordan May 17 '14 at 13:52
2

Since you've used the true argument for get_post_meta, $blogimages will be a single array, not a multidimensional one.

So instead of $blogimages[0]['blog-image-inside'], just use $blogimages['blog-image-inside'].

For absolute sanity, you might also want to check $blogimages before you use it:

'exclude' => [
    $thumb_ID,
    isset( $blogimages['blog-image-inside'] ) ? $blogimages['blog-image-inside'] : 0,
    isset( $blogimages['blog-image-front'] ) ? $blogimages['blog-image-front'] : 0,
    isset( $blogimages['blog-image-main'] ) ? $blogimages['blog-image-main'] : 0
],
| improve this answer | |
  • The true argument doesn't guarantee a single dimensional array. If blogimages was saved to a single key as a multidimensional array, it will be multidimensional coming back out. true only collapses the array in cases where values are saved multiple times under the same key name-- that is, multiple database entries. – s_ha_dum May 19 '14 at 13:16
  • Valid point. I jumped the gun and assumed how @Jordan had been saving data, and therefore what the mistake/misunderstanding was in this particular case :) – TheDeadMedic May 19 '14 at 13:25
  • But it looks like you were right :) – s_ha_dum May 19 '14 at 13:31

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.