最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Wordpress show Gallery Title and Captions

programmeradmin1浏览0评论

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;
}

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;
}
Share Improve this question edited Jan 7, 2015 at 7:48 KC Chai asked Jan 7, 2015 at 7:39 KC ChaiKC Chai 552 silver badges13 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 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/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/questions/19802157/change-wordpress-default-gallery-output

发布评论

评论列表(0)

  1. 暂无评论