0

I posted this on the regular stackexchange site, but then realized that there was a wordpress specific site, so I'm reposting it here.

I am trying to create a gallery within a custom post type I have created. I would like to be able to add the images/gallery to the post through the wordpress admin editor, but then have a function pull the images, wrap them in divs, and replace the existing gallery with the new images.

I would like to do the because I would like the images to fit into a grid of different sized images. For example, image 1 would be the full width, image 2 would be half the width, image 3 quarter and so on.

I have tried two methods, one being get_children()

$featuredImage = get_post_thumbnail_id( $post->ID );
$imageArgs = array(
  'numberposts' => 5,
  'order' => 'DESC',
  'post_mime_type' => 'image',
  'post_parent' => $post->ID,
  'post_type' => 'attachment',
  'exclude' => $featuredImage
);
$attachments = get_children($imageArgs, ARRAY_A);
$rekeyed_array = array_values($attachments);
$child_image = $rekeyed_array[0];
echo '<div class="image-large"><img src="' . $child_image['guid'] . '" class="project-image"></div>';
$child_image = $rekeyed_array[1];
echo '<div class="image-medium"><img src="' . $child_image['guid'] . '"></div>';
$child_image = $rekeyed_array[2];
echo '<div class="image-small"><img src="' . $child_image['guid'] . '"></div>';

and the other being get_post_gallery()

$gallery = get_post_gallery( get_the_ID(), false );

        foreach( $gallery['src'] AS $src )
        {
            ?>
            <div class="image-large">
            <img src="<?php echo $src; ?>" alt="Gallery image" />
            </div>
            <?php
        }

I haven't made much progress with the get_post_gallery(), but I sorta understand that I will have to use wp_get_attachment_url() to get the full sized images instead of thumbnails.

Now, two questions:

  1. I'm a little confused about arrays, so how would I go about selecting the first image in the array and wrapping it in a div with class "image-large" and then the second image and wrapping it in a div with class "image-medium"?
  2. How do I replace the gallery/images I have added through the editor with the new gallery/images? Right now, I get two instances of the images, the original added through the editor, and the images obtained through the functions.

EDIT

I figured out question 1, I think. Read up on associative arrays and realized you can do something like echo $gallery['src'][0]; to get the source url of each image. Still confused about question 2, though.

1 Answer 1

0

Figured it out.

//Remove original Gallery
function remove_the_first_gallery( $output, $attr ){
    $output = '<!-- gallery 1 was here -->';   // Must be non-empty.
    return $output;
}

add_filter( 'post_gallery', 'remove_the_first_gallery' );

That removed all galleries on the page. But since my new gallery isn't technically a post_gallery, it was left alone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.