WordPress.org

Ready to get started?Download WordPress

Forums

Skip first attachment on a get_children array (12 posts)

  1. omnialex
    Member
    Posted 2 years ago #

    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?

  2. Jackson
    Member
    Posted 2 years ago #

    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

  3. omnialex
    Member
    Posted 2 years ago #

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

  4. Jackson
    Member
    Posted 2 years ago #

    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.

  5. omnialex
    Member
    Posted 2 years ago #

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

  6. Jackson
    Member
    Posted 2 years ago #

    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; ?>
  7. omnialex
    Member
    Posted 2 years ago #

    Same result unfortunately.

  8. Jackson
    Member
    Posted 2 years ago #

    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; ?>
  9. omnialex
    Member
    Posted 2 years ago #

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

  10. CodeyMonkey
    Member
    Posted 2 years ago #

    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

  11. thereformation
    Member
    Posted 2 years ago #

    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
     }
    }
  12. CodeyMonkey
    Member
    Posted 2 years ago #

    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)

Topic Closed

This topic has been closed to new replies.

About this Topic