0
votes

I apologize if this is answered somewhere on the site, but I've been searching since yesterday and can't seem to find an answer.

I am trying to create a grid of different sized images in a custom post type. I would need the images to be added in the editor and not anywhere else. The custom post type has a featured image at the top that I created using the_post_thumbnail(); Below that is some text, and below that I have added some images. The text and images have been added through the editor in the admin panel.

I am not sure if this is the right way to go about it, but I've been using the get_children() function to grab all the images I've uploaded to the post. This is my code for that section:

<?php
$featuredImage = get_post_thumbnail_id( $post->ID );
$attachments = get_children( array( 'post_parent'=>$post->ID, 'post_type'=> 'attachment', 'exclude'=>$featuredImage ) );

    if ( $attachments ) {
    foreach ( $attachments as $attachment ) {
        $image_attributes = wp_get_attachment_image_src( $attachment->ID, 'thumbnail' )  ? wp_get_attachment_image_src( $attachment->ID, 'thumbnail' ) : wp_get_attachment_image_src( $attachment->ID, 'full' );

        echo '<img src="' . wp_get_attachment_thumb_url( $attachment->ID ) . '" class="project-image w3">';
    }
}
?>

This displays all the images except the featured image. How can I display only the second image, for example?

In the end I would like to make it so that I have an if statement that checks for number of images added, and based on the number of images in the post, apply different css classes to different images in the get_children array and display them, replacing the same images that I have added through the editor.

I really appreciate the help. Please let me know if I haven't been clear enough, or if I'm going about this the wrong way.

Thanks!

Update

I am trying to replace the images I added through the wordpress editor with the new images in their desired divs I created using the get_children() function. Is preg_replace() the way to go?

1

1 Answers

0
votes

The code snippet you are looking for is on the man page:

http://codex.wordpress.org/Function_Reference/get_children#Show_the_first_image_associated_with_the_post_and_re-key_the_array

What this does is puts all of the post IDs into an array called rekeyed_array

Then you can access the first, second, etc by referring to the array by changing the index (0) to what you are looking for.

$child_image = $rekeyed_array[0];  

Once you have this level of control, then adding the css classes should be fairly straightforward based on the number of attachments. If you want more details on this please ask.