Support » Plugins and Hacks » Hacks » Skip first attachment on a get_children array

  • I’m using get_children to display image attachments in a custom gallery, but I don’t want to display the first image as that is already shown as a bigger version. Someone suggested I could use ‘offset’ as an option but that doesn’t seem to work. Here is my IF conditional:

    <?php if ( $images = get_children(array(
    ‘post_parent’ => get_the_ID(),
    ‘post_type’ => ‘attachment’,
    ‘post_mime_type’ => ‘image’,
    ‘order’ => ‘ASC’,
    ‘offset’ => 1
    ))) : ?>

    then there’s a ‘foreach’ after that. Any suggestions?

Viewing 11 replies - 1 through 11 (of 11 total)
  • Try array_shift* before your foreach loop to remove the first item in the array.

    $images = array_shift( $images );

    *http://php.net/manual/en/function.array-shift.php

    Could you show me exactly where. Here is the code with the foreach in context:

    <?php if ( $images = get_children(array(
    ‘post_parent’ => get_the_ID(),
    ‘post_type’ => ‘attachment’,
    ‘post_mime_type’ => ‘image’,
    ‘order’ => ‘ASC’,

    ))): ?>

    <?php foreach( $images as $image ) : ?>

    ” href=”<?php echo wp_get_attachment_url($image->ID); ?>” rel=”lightbox”><img src=”<?php echo wp_get_attachment_url($image->ID, ‘thumbnail’); ?>”>

    <?php endforeach; ?>

    <?php else: // No images ?>
    <?php endif; ?>

    Replace this line:

    <?php foreach( $images as $image ) : ?>

    With

    <?php $images = array_shift( $images );
    foreach( $images as $image ) : ?>

    If that doesn’t work use wordpress.pastebin.com to post your code and I’ll look closer.

    I’m afraid that just returns twenty empty links. Anything else I can try?

    Ah yes, my mistake get_children returns an object by default so that will not work. Something like this, adding ARRAY_A to get_children, and changing object references to array – ie: $image->ID becomes $image[‘ID’]

    <?php
    if ( $images = get_children( array( 'post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC' ), ARRAY_A ) ):
       $images = array_shift( $images );
        foreach( $images as $image ) : ?>
        <a href="<?php echo wp_get_attachment_url($image['ID']); ?>" rel="lightbox"><img src="<?php echo wp_get_attachment_url($image['ID'], 'thumbnail'); ?>" /></a>
        <?php endforeach;
    else: // No images
    endif; ?>

    Same result unfortunately.

    My bad, just tested this:

    <?php
    if ( $images = get_children( array( 'post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC' ), ARRAY_A ) ):
       $remove = array_shift( $images );
        foreach( $images as $image ) : ?>
        <a href="<?php echo wp_get_attachment_url($image['ID']); ?>" rel="lightbox"><img src="<?php echo wp_get_attachment_url($image['ID'], 'thumbnail'); ?>" /></a>
        <?php endforeach;
    else: // No images
    endif; ?>

    Success! Many thanks Jackson, I owe you a pint!

    This doesn’t work in WordPress 3.3

    e.g. I want to skip the first image…which will be my featured image…and just show the other images attached to the post…

    But this no longer works as of WordPress 3.3. – do you have a solution?

    (I also use the “else” part to just show a “no Image” graphic, but as I say, it no longer shows since the update to 3.3

    There is usually no guarantee that the first image will be the featured image.

    What I do is find the ID of the featured image, if there is one, and then skip it when iterating over the returned array of attachments. Many ways to skip over it, following is fairly simple.

    if ( has_post_thumbnail($post->ID) ) {
     $id = get_post_thumbnail_id($post->ID);
    }
    foreach ($attachments as $attachment) {
     if($attachment->ID != $id) {
       //get your attachments
     }
    }

    Thanks very much, appreciated.

    So with 3.3 – how do you the check “If has children” (e.g. I want to show a “Placeholder” image if the post doesn’t have any attachments? (But does have a featured image)

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Skip first attachment on a get_children array’ is closed to new replies.