RSS

Wordpress – Use NextGen Gallery Code Inside an Excerpt

One problem with the ways that Wordpress handles excerpt is that it always strips the HTML out. This is fine for most casual bloggers but when you are really trying to customize something for a client, it simply doesn’t cut it. What we wanted to do was use some of the NextGen Gallery markdown code in the “Excerpt” box of the post and have Wordpress render this in the category archive.

After some initial research I discovered that this markdown code is called “shortcode” in WP terminology. They have a whole Shortcode API. Now it was just a matter of finding the correct functions to get the shortcode to render.

I wanted to create a special template for the category, essentially a photo blog, so that it would look different from the other categories. This is pretty straightforward, just create a new file called category-id.php, where id is the id number of your category. In my case it was category-40.php. The next step was to copy all the code from category.php and paste it into category-40.php.

When displaying the post’s excerpt, Wordpress uses a function called the_excerpt(). Here it is in the default sandbox template:


the_excerpt(''.__('Read More <span class="meta-nav">&raquo;</span>', 'sandbox').'') ;

The problem with the_excerpt is that is echoes the excerpt directly to the screen. In order to return the excerpt value as a string we need to use get_the_excerpt(). Next after checking the shortcode API, I discovered a function that will run all the shortcodes on a given string and return the output as a string, do_shortcode().

So all we need to do now is combine these two functions, and move it into the template:


echo do_shortcode(get_the_excerpt());

And there it is!

  • Share/Bookmark

Related posts:

  1. Make an Awesome functions.php File For You New Wordpress Template – Part 1 Its been awhile since I threw up a new blog...



2 Responses to “Wordpress – Use NextGen Gallery Code Inside an Excerpt”

  1. [...] for Google, and for bloggers who use good keywords and phrases in their writing. And thank you to Mummy.org for sharing how to use NextGen Gallery code inside a wordpress excerpt. 0 Comments No Comments [...]

Leave a Reply