• I have a nextgen slideshow gallery appearing on each post to display the post gallery. Each post has a custom field (carousel_ngen_gallery) that refers to a nextgen gallery which seem to be the same as the gallery_id field. At the bottom of the page I just want to display all the thumbnails of the pictures in the gallery for printing etc – in addition to the slideshow which appears at the top.

    I have written two different functions for functions.php to automatically show the gallery at the bottom of the post by using the custom field gallery_ id see below. Neither of them work though. I have clearly done something stupid but I am not sure what. Anyone got any ideas?

    Option 1. This one is only showing the post thumbnail and nothing else and the thumbnail url is not refering to the gallery directory at all.

    add_action('pagelines_loop_after_post_content', 'autogallerycube');
    
    function autogallerycube(){
    if( is_single($post)) {
    $gallery = get_post_meta($post->ID, 'carousel_ngen_gallery', true);
    $gal = $gallery;
    $gal = apply_filters('the_content', '[gallery='.$gal.']' );
    echo $gal;
    }
    }

    Option 2. This one displays this [Gallery not found.

    add_action('pagelines_loop_after_post_content', 'autogallerycube');
    
    function autogallerycube(){
    if( is_single($post)) {
     $gallery_id = get_post_meta( $post->ID, 'gallery_id', true);
      if( isset($gallery_id) ){
        echo "<div class=\"post-gallery\">";
        echo nggShowGallery( $gallery_id );
        echo "</div>";
    }
    }
    }

    But if I overwrite

    echo nggShowGallery( $gallery_id );   with
    echo nggShowGallery( 4 );

    It shows what I want – gallery 4 as thumbnails. So I have done something wrong when trying to recall the post_meta.

    Anyone see the error of my ways!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi,

    I think your main problem is that you are trying to use the $post variable outside of The Loop.
    Try to insert this line:

    setup_postdata($post);

    between:

    function autogallerycube(){

    and

    if( is_single($post)) {

    Also, this last line should be

    if( is_single($post->ID) {

    Thread Starter geez1

    (@geez1)

    Hi – I gave that a go but I still get the same gallery not found message. The code I used is given below. I am using platformpro and I can not work out where the single post loop is coded to edit it directly. Ideally I want to keep all my customisation in the functions.php file anyway.

    add_action('pagelines_loop_after_post_content', 'autogallerycube');
    
    function autogallerycube(){
    setup_postdata($post);
    if( is_single($post->ID)) {
     $gallery_id = get_post_meta( $post->ID, 'gallery_id', true);
      if( isset($gallery_id) ){
        echo "<div class=\"post-gallery\">";
        echo nggShowGallery( $gallery_id );
        echo "</div>";
    }
    }
    }

    I see. Perhaps we should just apply the filter to the_content and call $wp_query instead:

    add_action('the_content', 'autogallerycube');
    
    function autogallerycube($content){
      global $wp_query;
      $current_post_ID = $wp_query->post->ID;
      if ( is_single($current_post_ID) ) {
        $gallery_id = get_post_meta( $current_post_ID, 'gallery_id', true);
        if( isset($gallery_id) ){
          $content .= '<div class="post-gallery">'.nggShowGallery( $gallery_id ).'</div>';
          return $content;
        }
      }
    }

    You also need to have global $post; if you are using it inside a function.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Nextgen code for page template using gallery_id & post_meta’ is closed to new replies.