Inserting a NextGen gallery or slideshow in a custom field

I’m working on a project where I’m using custom fields to display NextGen galleries and slideshows separately from Wordpress’s main page content (it’s an unusual layout – I’ll post a link here once it’s ready for viewing). Searching for help led to info on using custom fields to set parameters for the galleries, which wasn’t what I wanted, and then I found this solution, which almost worked but not quite (it displayed some unwanted text).

Here’s how I eventually did it:

Install and activate the NextGen plugin. Create a gallery, add images and configure the options. Full instructions here: http://alexrabe.boelinger.com/wordpress-plugins/nextgen-gallery/.

Then:

  1. On the page on which you want the gallery to be displayed, create a custom field called gallery and, in the value box, enter gallery=1 (enclosed in square brackets!) or slideshow=1,560,280 (choose your own parameters) or whichever tag you want (polaroid, singlepic, album – see the gallery plugin page for options). Click update.
  2. In your theme’s page.php file (or whichever template file is relevant to your needs – I’m using pages), insert the following in the place where you want the gallery to appear:
    <?php $gallery = get_post_meta($post->ID, ‘gallery’, true); ?>
    <?php
    $gal = $gallery;
    $gal = apply_filters(‘the_content’, $gal );
    echo $gal;
    ?>
  3. Add the following to your theme’s stylesheet:
    .post-meta {
    list-style-type: none;
    }

    .post-meta-key {
    display: none;
    }

  4. Play with the stylesheet and template file further to position your gallery where you want it. I created a “picturebox” div to contain mine.
  5. Note that you can now display different galleries on different pages by changing each page’s “gallery” custom field value.

33 Responses to “Inserting a NextGen gallery or slideshow in a custom field”

  1. Ricardo says:

    Excelent! Thanks so much!

  2. Moose says:

    Genius!

    Been looking for this for ages.

    Thanks!

  3. WebDiggin says:

    Thanks for the help. Your method is very versatile because you can enter in the shortcode directly into the custom field. We always wanted to show the gallery associated with the page / post, so we modified your code so we only had to put the gallery number into the custom field. Appreciate your help.

    ID, ‘gallery’, true);
    $gal = $gallery;
    $gal = apply_filters(’the_content’, ‘[gallery='.$gal.']‘ );
    echo $gal;
    } ?>

    • Jasper says:

      Found the same code on WordPress.org’s forum as WebDiggin. Just need to add template=template-name to it and make it work using my own ngg template. At the moment the code with template=template messes up the loading od the ngg and shows just two thumbnails… Very odd. Ideas?

  4. trish says:

    This was exactly what I needed! Thanks so much!

  5. Thanks so much for this article! I only wanted the slideshow to show up on certain pages in the sidebar and couldn’t figure out how to do it until I found this.

    • omer says:

      Hi, I’m using your code to display a different slideshow for different pages on http://topsolar.co.il/about , but the slideshow (with only two very light weight alternating pictures) takes forever to appear on the page. can you please help me with that? I tried everything I could think of. replaced the slideshow with the smoothgallery, recreated the galleries, removed all other plugins, and whatnot.
      thanks in advance,
      Omer

      • admin says:

        Hi Omer. I like your website! I can see the problem but I’m not sure how to speed it up, sorry. Does it load faster if you don’t use the code and just display the same slideshow on each page?

        • omer says:

          thanks for replaying so quickly!
          I tried the slideshow widget on the sidebar, but got the same terrible slow results. the rest of the page loads quickly. I’m at lost. I’m trying to fix it for a few days now. do you know maybe some other way to display different slideshows for different pages?

          btw. where are you from?

          • admin says:

            I don’t know of a different way…Perhaps there’s an incompatibility with your Wordpress version? Have you tried asking the plugin author? (I’m sure you have.)

            I live in the Outer Hebrides of Scotland.

  6. Rakesh Ranjan says:

    I am new in wordpress. I want a flsh file display on my main web site. Please give me details how to do it…
    I maked a gallery….

  7. omer says:

    The Outer Hebrides of Scotland sounds far away and peaceful. I hope to get a chance to visit there.
    I’ll keep digging the web for an answer, maybe I’ll change cms. Thanks for your help. My blog is http://www.stainedarts.eu , you’re welcome to drop by,
    Omer – Israel

  8. Lori says:

    Hello,

    Thank you for the custom code. Works great on my site! I wanted to go a bit further and have the title and description of the gallery show up in a different div to the right of my slideshow.

    The template is all set up, so the div is there, but I have been having a really hard time finding the right code to insert so that the title and description of the gallery automatically show up when I insert the custom slideshow field on a page.

    Right now the .php file shows the following code, and only generates the name of the page in that div, which I don’t want:

    I also wondered if someone could help regarding borders around slideshows. I wanted to place a 10px wide border all around my slideshow and have tried numerous ways, including editing the ngg gallery.css, but the slideshow is never centered within the border.

    Thank you!
    Lori

  9. Lori says:

    Sorry to double-post, but forgot to leave the page on my site that I was referencing regarding the above post: http://tomasi-design.com/?page_id=7.

    Also, can’t seem to get the code to show up in the post. I’ll try again here, but it may not work.

    Lori

  10. Lori says:

    Figured out the border issue! Just added the border code into my style.css and made sure the div was the same width as my slideshow!

    Still need help with automatically pulling in the name and description of the gallery code as stated in the post above, though.

    Thanks for any guidance!

    • admin says:

      Hi Lori, I’m glad you fixed the border issue; your website looks excellent. I’m afraid I don’t have the time just now to look at the title/description issue – I hope you’re able to find a solution. Good luck!

      • Lori says:

        Thanks. I’ll keep investigating.

        Do you know if it’s possible to create custom fields to display gallery title and description?

  11. Christian says:

    Excellent walkthrough. Why is it that it’s not working for me, it’s such a simple three step walk through and still I managed to screw it up LOL.

    (Yes i added the square brackets haha )

    - CS

  12. Jeff Hajek says:

    Hi,
    I actually found this on Kim Woodbridge’s site, but she referenced you, so maybe you can answer my question.

    I think this code may be really useful for Wordpress to act as more of a true CMS. What I want to do is create a template that calls up the values from several defined keys and inserts them into the template.

    My application is for an online encyclopedia–the template defines the layout, and the values contain html with embedded shortcodes from a variety of plugins.

    I have it working to pull up the content in multiple locations (from multiple keys), but it also pulls up all the plugins I have that show up at the bottom of the post (i.e. add to any, microkids related posts, gd star rating). So those plugins show up multiple times in the page instead of just at the bottom.

    Any ideas why it would do that? I’m not a developer–I just muddle through with cut-and-paste technology. But I suspect it is either the location I am calling it up in my template, or it is the apply_filters–that I need to add additional commands, but I have had no success doing that.

    Any help would be great.
    Thanks,
    Jeff

    Thanks for saving the little bit of hair I have left. I’ve been pulling it out for two days before seeing this post, and now I am on the cusp of success, thanks to your brilliance.

    Hope you can help me out.

    I’m trying to make a template that can pull in blocks of content with embedded shortcodes–a variety of plugins–sniplets, download manager, notes, etc. (I do an online reference guide, and am looking for a uniform look for all the entries that I can easily change. Each section of content is assigned a key and I put the html in as the value)

    So, the problem I am running into is that the code is pulling up some of the plugins that show up at the bottom of the page–ratings and share this–every time I call up the value from the custom field. With two custom fields, I get three ’share this’ displays–the correct one and one after each value is shown.

    Any ideas on how to get rid of this?
    Thanks.
    Jeff

    • Jeff Hajek says:

      sorry–it’s late and I didn’t proof my work. The second part was my comment on Kim’s site. The brilliance applies to you as well…

      • Jeff Hajek says:

        There’s always more than one way to skin a cat. I just went in and switched the settings on all my plugins and put them into the templates manually.

        Regardless, thanks for the bit of code–it works great now, and will let me do what I need to.

        • admin says:

          Sorry I wasn’t able to help, Jeff – I’m just really busy at the moment. I’m glad you figured out a way to do what you want.

  13. [...] Scotproof’s: Inserting a NextGen gallery or slideshow in a custom field [...]

  14. Jack says:

    This worked well. Thanks for the well written, easy to use tutorial

  15. Nicola says:

    Im running smooth gallery for nextgen and no matter what i do i cant get this to work…. any suggestions?? I basically want to put it in a custom template which has 1 column for the text and 1 for the gallery and a series of other un-editable images (which are hard coded into the template)

    at the moment the code simply sits on the page and is floated right, but obviously i need to make it into a workable column.

    Thanks!!!!!!!!

  16. Greg Shaw says:

    Very useful this. Just what I’ve been looking for. I’ve struggled with the PHP to do this and hey presto, a quick search and here it is. Thanks a lot :)

  17. Jayme Cochrane says:

    I can’t seem to get this to display a different gallery type other than the standard. Trying to get the [template=galleryview] tag integrated, but no luck so far (I’m not so good with PHP). Any ideas?

    Thanks!

  18. Hi

    I am trying to rebuild a site (moving from one server to another) and before I hit go I am trying to integrate your code into Thesis and use the multimedia box. However, all I get as an output is ID, ‘gallery’, true); ?>. It is obviously missing something. Sorry I can’t post a link – it is on a test install.

    What I was hoping to do was dump the code you had in the multimedia box (part of Thesis) complete without using the extra field element.

    Is that possible please?

    Thanks

    Rich

  19. rik says:

    hi great stuff! just a not while copying and pasting your code into my php editor, it placed wrong comma’s in the code. maybe its only me but just to let you know.

  20. Sjoerd says:

    I also would like to compliment you on this. You are so far the only one who had a clear and clean solution! But I’d like to add a warning for all the people like me who suck at php (learning slowly though). What Rick says here above happened to me too, so it’s not just you Rik. If you copy paste the code the comma’s are wrong!, make sure you paste it like this:

    ID, ‘gallery’, true); ?>

    paste it inside the loop and it will work.

  21. Sjoerd says:

    PS:haha, sorry ignore my example because it automatically put the wrong comma’s in my reply. Just copy the code and put the straight comma’s in yourself…

  22. Michelle says:

    Thank you! You’re code is brilliant. :)

Leave a Reply