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?

Tutorial details
DifficultyEasy (rss)
Root privilegesNo
RequirementsWordpress
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:

ADVERTISEMENTS


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');
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
0 comments… add one

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.