Nerds at Work

RSS

How to Make Shortcodes Work in WordPress with the Excerpt

Written by Brian on August 12, 2010 – 6:06 pm -

I’m working on a new theme for another project (Olinda Gibbons Photography), and I ran into a little problem. I wanted to create an excerpt of each post for use in the index page, and I wanted the full post to appear in the single page (single.php). Simple enough.

But, a key part of the concept was to include a Nextgen Image Gallery in that excerpt. It’s a photography website, and the point of the blog posts is to show pictures. Seems kind of silly to show a text excerpt without the image gallery, eh? I love the Nextgen gallery because it lets me easily upload pictures to the server, organize them into galleries (i.e. job for clients) and then post them on a WordPress page/post with a single piece of shortcode.

The problem is that this shortcode will, by default, execute in the post (when you call the function the_content()), but it won’t execute in the excerpt (when you call the function the_excerpt()).

I searched around and found a simple solution. Open up (or create) a “functions.php” within the root folder of your theme. Insert these two lines of code…

add_filter( 'the_excerpt', 'shortcode_unautop');
add_filter( 'the_excerpt', 'do_shortcode');

This tells WordPress to execute any shortcodes that it finds when you call the_excerpt(). Just what I was looking for…

Posted in CMS Tricks, Nerds at Work | 6 Comments »
Tags: ,

5 Comments

  1. Joshua Guffey, January 28, 2011:

    Tried this on a WP 3.0.4 install and couldn’t make it work. What version are you on? Thanks! :)

  2. chris, July 18, 2011:

    No dice for me either. I’m using 3.2

  3. Mike, August 16, 2011:

    This works only if the excerpt was defined (using the more tag). If not WordPress pulls out by default 55 first characters and this solution would not work. It is very frustraing, damn you WordPress team!

  4. istanbul Escort, September 12, 2011:
  5. istanbul Escort, September 12, 2011:

1 Trackbacks/Pingbacks

  1. Pingback: Thumbnail or Nextgen gallery in excerpt « Michiel van Dijk
    October 7, 2010

Leave a Comment