WordPress.org

Ready to get started?Download WordPress

Forums

How to get a gallery as an array (4 posts)

  1. marianograndioso
    Member
    Posted 1 year ago #

    I'm working on a website where I use a custom post type with lots of custom fields, multiple galleries and a featured image. One gallery uses a jquery plug-in, the other one uses the gallery shortcode.

    At first, when I only had the jquery gallery, I simply grabbed all images attached to the given post, excluded the featured image and chucked them in a mark-up that the plug-in uses with this code:

    $a_args = array(
        	'post_type' => 'attachment',
        	'post_parent' => $post->ID,
        	'posts_per_page' => -1,
        	'post_mime_type' => 'image',
        	'exclude' => get_post_thumbnail_id( $post->ID ),
        );
    
        $a_images = get_children( $a_args );

    However, now that I've added another gallery (this one is only output with the gallery shortcode) my solution doesn't work as desired, because it takes all the images - even the ones from the other gallery.

    So ideally I would like to get an array of images that would be output by the gallery shortcode as simply as possible.

    How do I do this ?

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    With this function in your theme's functions.php you can get the attachments from all the galeries in the post content: http://pastebin.com/xVvKZFtU

    In your theme template files inside the loop you can use the function like this:

    <?php
    $galleries = get_gallery_attachments();
    
    if($galleries) {
      foreach ($galleries as $gallery) {
        foreach ($gallery as $attachment) {
         // do something with the attachment object
         echo $attachment->ID;
        }
      }
    }
    ?>

    outside the loop you'll need to give it an post object or id:

    <?php
    // get gallery attachments for post id 23
    $galleries = get_gallery_attachments(23);
    ?>
  3. marianograndioso
    Member
    Posted 1 year ago #

    Whoa, that's a big chunk of code and it's not exactly what I was looking for. It's my fault though, because I thought the gallery shortcode took the gallery's ID as parameter, when it's actually using the post's ID. I just realised the galleries themselves don't have IDs. Such a shame. They should make galleries another post type.

    What I'll do now is I'll make the images that used by the shortcode not be attached to the post itself. I can still include them in the gallery and they won't show up in the other one where I take all the images from the post.

    Thank you for your time, keesiemeijer.

  4. keesiemeijer
    moderator
    Posted 1 year ago #

    Whoa, that's a big chunk of code

    It's a stripped version of what the WordPress core function gallery_shortcode() does (for every gallery shortcode). With WordPress 3.6 we can filter the shortcode for the attachment ids:
    http://markjaquith.wordpress.com/2013/04/04/wordpress-36-shortcode-attribute-filter/
    But I haven't looked into it yet.

    I'm glad you found a solution.

Topic Closed

This topic has been closed to new replies.

About this Topic