How to use Nextgen gallery using custom php code on theme file

On nextgen gallery, we use [nggallery id=x] short tag on post/page to display our desired gallery. To display desired gallery on any place in a theme, we can use the following custom code:

             $newnggShortcodes = new NextGEN_Shortcodes;
             echo $newnggShortcodes->show_gallery( array("id"=>$key,"images"=>x,"template"=>"popular") );

Where $key is gallery id
x is number of gallery image to be displayed on that page
Popular is the template to display gallery (generally it will be inside nextgen-gallery\view\gallery.php), But you can create your own. For that creat copy of that gallery.php and rename it as gallery-{template name}.php. Here we are using template name as popular so the template file will be gallery-popular.php

Similarly for album we can use on theme file as

	$newnggShortcodes = new NextGEN_Shortcodes;
        echo $newnggShortcodes->show_album( array("id"=>$albums_id, "template"=>"extend") );

Here, $albums_id is id of album.
Template is same as above ie it uses nextgen-gallery\view\album-extend.php
or on post/page we can use [album id=x template=extend] or [album id=x template=compact] on content section

Similarly for single picture thumbnail we can use

	$newnggShortcodes = new NextGEN_Shortcodes;
         echo $newnggShortcodes->show_thumbs( array("id"=>$picture_id, "template"=>"") );

Here, $ picture _id is picture id.
Template is same as above ie it uses nextgen-gallery\view\imagebrowser.php by default. We can create own template same as above.
And for single picture we can use

	$newnggShortcodes = new NextGEN_Shortcodes;
echo $newnggShortcodes->show_singlepic( array("id"=>$picture_id,"w" =>'',"h"=>"","mode"=>"","float"=>"","link"=>"","template"=>"") ); 

or on post/page we can use [singlepic id=x w=width h=height mode=web20|watermark float=left|right]on content section

Comments

comments

12 Comment

  1. Great post. Having problems getting this to work on one of my pages and I have a feeling it is because there is other php business going on with that page. Here is the page where I am trying to do this – http://www.sfpgagolf.com/junior-golf/. You will see at the bottom of the page where they are. Everything is working fine on the regular gallery page, just can’t seem to get this to work on this page. Any help is much appreciated. Thanks much.

    1. vinay says: Reply

      Send me detail in mail so that i can have a look. Thanks

  2. Just another visitor says: Reply

    Hey there..

    I was searching for a way to do this when google kindly directed me towards your blog.

    Thanks a lot for posting this up, reading this gave me a nudge towards the solution I needed. Awesome!

    I’ll just add a bit of info here as well, I wanted to add a NexGen ‘slideshow’ in one of my template files.
    I was able do this using your code and changing a few pieces

    show_slideshow( array(“id”=>$key, “h”=>$height, “w”=>$width) ); ?>

    where $key is the ID of your gallery, and $height and $width is the height / width of your slideshow respectively.

    Thanks again for the blog post, just what I needed 😉

  3. Just another visitor says: Reply

    Hey there..

    I was searching for a way to do this when google kindly directed me towards your blog.

    Thanks a lot for posting this up, reading this gave me a nudge towards the solution I needed. Awesome!

    I’ll just add a bit of info here as well, I wanted to add a NexGen ‘slideshow’ in one of my template files.
    I was able do this using your code and changing a few pieces

    $newnggShortcodes = new NextGEN_Shortcodes;
    echo $newnggShortcodes->show_slideshow( array(“id”=>$key, “h”=>$height, “w”=>$width) );

    where $key is the ID of your gallery, and $height and $width is the height / width of your slideshow respectively.

    Thanks again for the blog post, just what I needed 😉

  4. Touch of Tulip says: Reply

    Hi! I’ve been breaking my brains over this and I’m hoping that you can help me out.

    I created 3 custom pages for a client in WordPress, and they are linked correctly as I can pick them up through the WP pages tab. On each page, I’d like to have 50 albums with 3 pictures each inside (as a gallery or subalbum, I assume). Then when one clicks on say album 1 a page with the 3 pictures appears with some information and a ‘add to cart’ or ‘buy now’ button.

    Right now, I’m only able to display a gallery with 3 images on the main page by using:

    ID, ‘gid’, true);
    $gal = $gallery;
    $gal = apply_filters(‘the_content’, ‘[gallery=4]’ );
    echo $gal;
    ?>

    I’m a PHP and WP newbie (though I know HTML/CSS) and I’m confused on when and how to use shortcodes (or even if I should). I tried using your code, but I can’t seem to add anything in the WP page field…do I need to create a custom field? If so, I don’t know how!

    Let me know if you can assist and what other information you need to solve my issues. Thank you so much!

    1. vinay says: Reply

      Hi, send me ftp details along with wp-admin info.

  5. ottie says: Reply

    Hi, thanks for the code clarification.
    One thing that keeps bugging me on the nextgen gallery:
    When I make a custom album template and call it, thumbs(of each gallery) are made with <a href="pagelink ?>”>, which opens the gallery with the standard gallery.php. How can this be changed in order that they open with my own custom gallery-custom.php ? I’m working with different languages and I need to change all the texts/title, but I can’t get it straight for the moment.
    Thanks a lot.

  6. Michael Julian says: Reply

    Is there a way to nextgen to insert the custom templet in to a page when nextgen “Add Page”?
    Now it adds the page with [nggallery id=2] when it builds a page. I would like it to add [nggallery id=2 template=xxx]

  7. Sascha says: Reply

    Hi there,

    those examples you have given are really helpful! I am not using Nextgen yet, but I am looking into its possibilities. One question you might have an answer too:

    is there a way to create a nextgen gallery with a php function only? If so, what information do you need to pass to the function? Any help is appreciated!

  8. David says: Reply

    HI Vinay,

    This is in response to an old post but I can not find much other help on the web, hopefully you are still about!

    I am less experienced than your regular users and am struggling to get this achieved.

    What I am building is a site (not blog) which uses custom page templates which I will build externally. At the moment they only have the get_header and get_footer calls and I will use them as templates when I create new pages via WP Dashboard. I will create a custom template for each page on my site. My welcome page will have a slideshow gallery of 6-10 images. I want to use NextGen gallery to do that.

    I have installed the NextGen plugin and created a gallery in WP (gallery id =1) but If I place the following code in a custom page being used as a template my site goes down, so there is obvious problems.

    show_gallery( array(“id”=>1,”images”=>6,”template”=>”twentyeleven”) );
    ?>

    I created the gallery-twentyeleven.php file in the /wp-content/plugins/nextgen-gallery/view directory but it will error.

    I have also tried it in a theme directory as follows:
    /wp-content/themes/twentyeleven/nggallery/

    I tried adding the following to the gallery-twentyeleven.php file header as I do the same to page templates, still no luck in either location:

    Can you see where I am going wrong?

    Thanks in advance,
    David

    1. vinay says: Reply

      Hi David,
      I have checked on my local server by creating a custom template page and a gallery-twentyeleven.php inside view folder. Then I have used the following code as :

      $newnggShortcodes = new NextGEN_Shortcodes;
      echo $newnggShortcodes->show_gallery( array("id"=>1,"images"=>6,"template"=>"twentyeleven") );
      

      It works without any error.
      Let me know what is the error you have faced while putting that code. You can activate error by putting

      define('WP_DEBUG', true);
      

      on your wp-config.php file.

  9. ottie says: Reply

    Hi Vinay,
    First of all a happy New year (we are the 1st today)!
    Now my question: as a programmer and nextgen user, you must be the one to turn to. I twittered and linkedin-ed it, but didn’t get any real answers.

    I’m building a site where I want to use different nextgen gallery templates. There are shortcodes to call a template, I know.

    I am using 2 or 3 custom album templates (on different pages): they show me their galleries. But when I click the gallery preview image, every album template calls back to the standard gallery.php to show the gallery.
    I’d like each custom album template to call a custom gallery template (of my choice), not the default. I’ve been checking the php, but couldn’t come up with a solution.
    Do you have any ideas on how to solve this?

    thanks a million
    Ottie

Leave a Reply