0

The only way I've been able to get the "current" gallery images from a post has been through "if has_shortcode" and checking for a gallery and getting the gallery images.

In this way the client can edit the wordpress gallery -- delete and add images and have the changes show successfully on the web page.

The problem is I can not use get_the_title of the image with this approach because so far, I believe, I've just needed the post ID.

So what I want is for the code to check if there is a gallery, and if so get the current gallery images, and also get the IDs of the images so that I can use "get_the_title" of the image and the title will display in the prettyPhoto lightbox.

here's my current code --

<?php if (have_posts()): while (have_posts()) : the_post(); ?>

    <div class="gallery-wrapper three-cols clearfix">

     <?php // image gallery content 
         if( has_shortcode( $post->post_content, 'gallery' ) ) {   

        a// Add a filter for full gallery size:
       add_filter( 'shortcode_atts_gallery','wpse_full_size_gallery' );


$gallery = get_post_gallery_images( $post->ID );

       // Remove the filter:
       remove_filter( 'shortcode_atts_gallery','wpse_full_size_gallery' );  

foreach ( $gallery as $image_url ) {


            echo '<div class="one-third columns gallery-item">';
            echo '<div class="item-picture" data-type="image">';
            echo '<img src="' . $image_url . '">' ;
            echo '<div class="image-overlay">';
            echo '<a href="' . $image_url . '" data-rel="prettyPhoto[gallery]"><span class="zoom"></span></a>';
            echo '</div>';
            echo '<span class="item-label">';
            // I need to get the attachment title
            echo '</span>';
            echo '</div>';
            echo '</div>';

        }

    }                         
?>

  </div>
        <!-- article -->
      <article class="full-width columns" id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <br class="clear">

            <?php edit_post_link(); ?>

        </article>
        <!-- /article -->

    <?php endwhile; ?>

    <?php else: ?>

        <!-- article -->
        <article>

            <h2><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h2>

        </article>
        <!-- /article -->

    <?php endif; ?>            

BTW ...the code example below is my first effort, but if the client edited the gallery, deleted and/or added images, the edits wouldn't appear on the web page (only the first images attached to the gallery). From what I've read here, images are always associated with a post unless deleted, or somehow scrubbed to be set back to "0"

I've also tried a version of this code from a post found here.

<?php if (have_posts()): while (have_posts()) : the_post(); ?>
 <div class="gallery-wrapper three-cols clearfix">

 <?php    

$gallery = get_children(array('post_parent' => $post->ID, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'post_status' => 'inherit', 'posts_per_page' => -1, 'orderby' => 'modified' ));

if ($gallery) {

   foreach ( $gallery as $attachment_id => $image ) {

            echo '<div class="one-third columns gallery-item">';
            echo '<div class="item-picture" data-type="image">';
            echo wp_get_attachment_image($attachment_id, 'gallery-size');
            echo '<div class="image-overlay">';
            echo '<a href="'. wp_get_attachment_url( $attachment_id ) .'" data-rel="prettyPhoto[gallery]"><span class="zoom"></span></a>';
            echo '</div>';
            echo '<span class="item-label">';
            echo get_the_title( $attachment_id );
            echo '</span>';
            echo '</div>';
            echo '</div>';

    }

    } // end see if images

?>
    </div> <!-- gallery wrapper -->

<!-- article -->
<article class="full-width columns" id="post-<?php the_ID(); ?>" <?php   post_class(); ?>>

            <br class="clear">

            <?php edit_post_link(); ?>

        </article>
        <!-- /article -->

    <?php endwhile; ?>

    <?php else: ?>

        <!-- article -->
        <article>

            <h2><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h2>

        </article>
        <!-- /article -->

    <?php endif; ?>            
  • have you looked at get_post_gallery_images in source? check the other related functions in there. – Milo Apr 13 '15 at 17:52
  • Isn't that what I'm using - checking to see if there is a gallery and if so, get the post gallery images. Now I need to go back to the books and figure out how to add to this code to get the image id and its title. – ccbar Apr 14 '15 at 19:05
  • I guess I could go back to the old code and ask client to delete the photo - especially if he isn't using it, why keep it. – ccbar Apr 14 '15 at 19:09
  • if you look at the source file that function is defined within, you'll see a number of other functions that will probably give you what you need. – Milo Apr 14 '15 at 19:33
  • right, I've looked, it accepts a post, or an object, and I'm trying to figure out how an object could help -- but yes I've checked and the code I'm using is based on what I found for get post gallery images: [codex.wordpress.org/Function_Reference/get_post_gallery_images] – ccbar Apr 15 '15 at 1:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.