Blog

03/06/2019

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

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

20/08/2019

Start your Bitcoin Exchange with our Software

Start your Bitcoin Exchange with our Software Setup your white label bitcoin exchange right away. Or create your own cryptocurrency using...
14/08/2019

How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...
14/08/2019

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....