≡ Menu

WordPress Add Shortcodes To Excerpts

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

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:
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');

{ 0 comments… add one }

Leave a Comment

   Tagged with: , , , , , ,