0

Is there a function that will let me get the post id of the first child (of the same post type, not attachment)? I could always just run a loop via wp_query but that seems like overkill. :/

The goal here is to build a function that displays the first attached image. My function goes through the post_thumbnail and the attached images of the parent post fine, but stops working when the only images reside in child posts.

Edit: Here is my current working function. It works, but its not pretty. How can I clean it up?

/****************************************/
/* Grab first image of a post
/****************************************/  
function surfbird_grab_album_image() {
    global $post, $posts;

    $albumtitle =  get_the_title();

    if(has_post_thumbnail()) :
        return the_post_thumbnail('small');

    else :

        //Grab image if parent post has one
        $args = array(
            'order'          => 'ASC',
            'orderby'        => 'menu_order',
            'post_type'      => 'attachment',
            'post_parent'    => $post->ID,
            'post_mime_type' => 'image',
            'numberposts'    => 1,
        );

        $attachments = get_posts($args);
        if ($attachments) :
            foreach ($attachments as $attachment) {
                echo wp_get_attachment_image($attachment->ID, 'small', false, false);
                echo apply_filters( 'the_title', $albumtitle );
            }
        else :

            //Search the child posts and grab the first image we find
             $albumgroups = get_posts( array( 'post_type' => 'album', 'post_parent' => get_the_ID(), 'orderby' => 'menu_order', 'numberposts' => -1 ) );
            foreach( $albumgroups as $post ) :  setup_postdata($post); 

            $postID = $post->ID;
            $args = array(
                'numberposts' => 1,
                'order'=> 'ASC',
                'post_mime_type' => 'image',
                'post_parent' => $postID,
                'post_status' => null,
                'post_type' => 'attachment'
            );

            $attachments = get_children( $args );

            if ($attachments) {
                foreach($attachments as $attachment) {
                    return wp_get_attachment_image($attachment->ID, 'small', false, false);
                }
            }
        endforeach;
        wp_reset_query();   

        endif;

    endif;
wp_reset_postdata();
}
1
  • post your function, maybe we can alter it a bit – Bainternet Oct 3 '11 at 13:53
1

Instead of:

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

Use:

    $args = array(
        'order'          => 'ASC',
        'orderby'        => 'menu_order',
        'post_type'      => get_post_type($post),
        'post_parent'    => $post->ID,
        'numberposts'    => 1,
    );

You could also use post/page/etc or 'any'

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.