WordPress CMS modifications
Making WordPress the ultimate CMS!

Mar/09

11

Inserting WordPress shortcode outside the loop in a template

Shortcodes were properly introduced back in WordPress 2.5 – they are usually used in the post content area for doing all sorts of cool things, both with core code and plugins… but what if you need to call a shortcode OUTSIDE of a post (loop) in your template?

You insert WordPress shortcodes like this [shortcode option1="value1" option2="value2"] into your main content area- but if you want to use WordPress shortcode outside of the loop in your template use the following PHP:

 <?php echo do_shortcode('[shortcode option1="value1" option2="value2"]'); ?>

4 comments

  • Stanley @ SEOAdsenseThemes.com · April 13, 2009 at 2:32 pm

    Thanks. Just what I was looking for right this very moment. Came in real handy indeed.

  • Mariostella · April 19, 2009 at 3:18 pm

    Argh! I’ve been battling with this for days…it does not work for me…it simply echoes the shortcode, as if no php were involved! I am trying to use your function call with NGG but I just get [slideshow=255] on my page. Why? Thanks

  • Jadeb · April 21, 2009 at 1:03 pm

    Thanks for this short but very useful posts. Most plugin authors assume that everyone wants their content styled on a post by post basis. Some of us,by reason of convenience or sheer laziness would like to implement some of these things sitewide. It would be nice if we were given information about how to execute these functions from our template files and not have to google all over the place lookin for that information.

  • Admin comment by Jonny · April 23, 2009 at 9:10 am

    @Mariostella – not sure why that is not working, but I have used this in template files and it has worked for me. Maybe it is something to do with the plugin (Next Gen Gallery?), take a look at the comments and support for this plugin – I would have thought that there must be a way to call a gallery outside the loop without needing to use shortcode.

    @Jadeb – The WordPress Codex is building up slowly but surely, but I know what you mean – a-lot of the time I have to go searching around the net for information – hopefully sites like mine help fill the gaps whilst the Codex is built up, and it’s a great reference for me too!

Leave a Reply

<<

>>