• Hello,

    I am using NextGen gallery in a project for a client. The client wants to be able to add galleries to their posts/pages (no problem), but also wanted an excerpt of the gallery to show up anytime an excerpt of the post is displayed. The core WordPress functions do not recognize the NextGen gallery, and after hours of searching howto do this to no avail (maybe i just suck at searching) I decided to code a function that does this.

    Here is my solution which shows excerpts of a NextGen gallery in a post/page excerpt:

    in your themes function.php add:

    function ngg_excerpt(){
    //get the post content
    $content_data = get_the_content()
    //extract shortcode from content
    preg_match("/\[ngg([^}]*)\]/", $content_data ,$matches);
    $results = $matches[1];
    //if shortcode exists in content
    if (!empty($results)){
    //extract gallery id from shortcode
    $gallery_id = preg_replace("/[^0-9]/", '', $matches[1]);
    //make sure that NextGen is loaded
    if (function_exists(nggShowGallery)){
    //output gallery, showing only 4 images
    echo nggShowGallery( $gallery_id, null , 4 );
    }
    }
    }

    `
    And then inside your loop just call ngg_excerpt(); wherever you want the gallery excerpt to output

    Please note that the excerpt will show pagination links, which will be broken. I’m still trying to workout a solution to not display pagination links. Any insights are appreciated. I will post pagination solution once I figure it out…

    Cheers

Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter redcam

    (@redcam)

    Also, this does not take into account that you may have other short codes in posts. If that is the case you would need to use a general expression that filters out [ngg] as opposed to [].

    Nevermind, I edited the above to only grab NextGen shortcode

    Thanks a lot for posting this!

    would love to use this but the code above breaks the site, is there anything missing in the code above??

    thanks!

    Line 3 was missing a the semi-colon .. it should be

    $content_data = get_the_content();

    then it will work

    Also to get rid of the broken image links, pagination and slideshow piece, make a new template to call in nggSHowGallery (i called mine gallery-compact.php) in which you remove the link, pagination and slideshow code.

    then upload new template to ngg .. \View folder

    then reference the new template in redcam’s code .. change:

    echo nggShowGallery( $gallery_id, null , 4 );

    to:

    echo nggShowGallery( $gallery_id, “compact” , 4 );

    (note you enter only “compact” bc it appears that nextgen code will parse it out eg it will recognize “compact” as “gallery-compact.php”)

    also, since the links are removed the ngg-gallery-thumbnail style has a link hover that shades that thumbnail box gray .. i made new set of styles named “ngg-gallery-thumbnail-nolink” in which i removed that hover so doesn’t turn gray when mouse over : )

    Thanks for the reply, however i just keep getting “[Gallery not found]” when i call ngg_excerpt(); in the loop. What could be wrong?

    Here’s the code i tried from my home.php to call the function…

    <?php get_header(); ?>
    
        <div id="content" class="narrowcolumn">
    
        <?php if (have_posts()) : ?>
    
            <?php 
    
            while(have_posts()) : the_post(); if(!($first_post == $post->ID) && !is_sticky()) : ?>    
    
                <div class="post lastfive" id="post-<?php the_ID(); ?>">
                <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php printf(__('Permanent Link to %s', 'chaoticsoul'), get_the_title()); ?>"><?php the_title(); ?></a></h2>
                    &bull;<?php the_time(get_option("date_format")); ?>  &bull; <?php comments_popup_link(__('No Comments', 'chaoticsoul'), __('1 Comment', 'chaoticsoul'), __('% Comments', 'chaoticsoul')); ?> <?php edit_post_link(__('Edit', 'chaoticsoul'), '(', ')'); ?>
                    <div class="entry">
                        <?php ngg_excerpt(); ?>
                        <?php the_content("<span class=\"continue\">" . __('Continue reading', 'chaoticsoul') . " '" . the_title('', '', false) . "'</span>"); ?>
    
                        <p class="postmetadata"><?php printf(__('Posted in %s', 'chaoticsoul'), get_the_category_list(', ')); ?>
                            <?php the_tags('<br />' .  __( 'Tags' ) . ': ', ', ', '');  ?>
                        </p>
                    <div class="splitter">&nbsp;</div>
                    </div>
                </div>
    
            <?php endif; endwhile; ?>

    and in functions.php i added the function like this:

    function ngg_excerpt(){
    //get the post content
    $content_data = get_the_content();
    //extract shortcode from content
    preg_match("/\[ngg([^}]*)\]/", $content_data ,$matches);
    $results = $matches[1];
    //if shortcode exists in content
    if (!empty($results)){
    //extract gallery id from shortcode
    $gallery_id = preg_replace("/[^0-9]/", '', $matches[1]);
    //make sure that NextGen is loaded
    if (function_exists(nggShowGallery)){
    //output gallery, showing only 4 images
    echo nggShowGallery( $gallery_id, null , 4 );
    }
    }
    }

    Just keep getting [Gallery Not Found] :/

    It seems galleries created with [ ] / \ in their names will not work to be displayed, any solution for this?

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘How-to show NextGen gallery in excerpt limit img count’ is closed to new replies.