1

I would like to show the gallery title together with the captions. Below is my attempt, but I keep getting Notice: Trying to get property of non-object . I still can't figure it out what went wrong.

single.php

<div class="gallery-content">
        <?php 
        add_filter( 'shortcode_atts_gallery', 'fullsize_gallery' );     
        if( has_shortcode( $post->post_content, 'gallery' ) ) :   

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

        $image_list = '<ul class="galleryslider" style="display: none;">'; 

        foreach( $gallery as $image ) :
        $image_title = wp_get_attachment_url($image->post_title);
        $image_caption = wp_get_attachment_url($image->post_excerpt);
        // Loop through each image in each gallery
        $image_list .= '<li><a href="'. $image . '" data-lightbox="'.$post->ID.'"><img src="' . $image . '" title="'.$image_title.'"/>'.$image_caption.'</a></li>';
        endforeach; 
        $image_list .= '</ul>';                     
        echo $image_list;                           
        endif;
        ?>
    </div><!-- .gallery-content -->

functions.php

function fullsize_gallery( $out )
{
    remove_filter( current_filter(), __FUNCTION__ );
    $out['size'] = 'full';
    return $out;
}
0

1 Answer 1

1

get_post_gallery_images only returns a singular array of image URLs, so $image->post_title etc won't work, hence your error message.

The approach generally is off - you've followed the codex for get_post_gallery_images but that snippet is aimed at re-using the image urls within the post, but outside the gallery output.

get_attached_media is more suited here (just a more concise helper than writing $args out for a get-posts call). http://codex.wordpress.org/Function_Reference/get_attached_media

If this is a modification to all your galleries then the answer below will show you a much better and more flexible approach (using the post_gallery filter instead). https://stackoverflow.com/questions/19802157/change-wordpress-default-gallery-output

2
  • Hi Steve82, I have seen the links provided but I still couldn't find my answer. Could you please show me more example?
    – KC Chai
    Commented Jan 7, 2015 at 17:48
  • Have you tried restoring single.php as it came with your theme originally and then copying the code example in the last link into your functions.php? That would get you started. If you let me know what theme you're using I'll try update the answer with a suitable example.
    – Steve82
    Commented Jan 8, 2015 at 1:15

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.