WordPress Add Shortcodes To Excerpts

by on November 5, 2012 · 0 comments· LAST UPDATED November 5, 2012

in

I added the Shortcode API as described here, but it is only displayed in post content. How do I use shortcode in wordpress excerpt?

Tutorial details
DifficultyEasy (rss)
Root privilegesNo
RequirementsWordpress
Estimated completion timeN/A

Shortcode is nothing but a simple set of functions for creating macro codes for use in post content. Shortcodes are written by providing a handler function. Shortcode handlers are broadly similar to WordPress filters: they accept parameters (attributes) and return a result (the shortcode output). By default shortcode will be displayed in the post content. You need to add a filter to the_excerpt function using the following syntax:


add_filter('the_excerpt', 'do_shortcode');

Note: Make sure your theme is using the_excerpt, other wise the following example will not work.

Sample example

Add the following to your functions.php

 
// Shortcode syntax:
//[bar]
function bar_demo( $atts ){
 return "This is a demo for wordpress shortcut.";
}
 
// add to the post 
add_shortcode( 'bar', 'bar_demo' );
 
// add to the excerpts
add_filter('the_excerpt', 'do_shortcode');
 
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 0 comments… add one now }

Leave a Comment

Tagged as: , , , , , ,

Previous Faq:

Next Faq: