WordPress Shortcodes: How to use Shortcodes in WordPress Theme Examples
Using Shortcodes in WordPress themes: If you are wondering how to use Shortcodes in WordPress Themes, then this article is for you. In this article, I am going to show you how easily you can use shortcodes in any of the existing WordPress themes or a brand new WordPress theme. Read on to find out more.
How to use Shortcodes in WordPress Theme
Before we move on, let’s quickly try to understand what a Shortcode is.
It is simply a line of code that when used in conjunction with WordPress theme, widget, plugin, etc. executes the associated function, performs the related operations as written in the code & shows the output result. That’s it for the definition.
So how to actually make use of Shortcodes in WordPress Theme?
There are couple of ways to do this. Following are the examples:
Assuming the want to use a shortcode named [my_shortcode] in a WordPress theme, simply copy & paste the following line of code in your theme where you want the output result of the shortcode to appear:
<?php echo do_shortcode("[my_shortcode]"); ?>
By mentioning the do_shortcode() command explicity, you thereby indicate to WordPress that you are trying to use a shortcode & thus make it honor your request. In order for this to work, you must have the relevant function by the name my_shortcode in your functions.php file (if you are using only a theme) or in the necessary php file (in case you are using the shortcode in a plugin).
Here’s an extended example. Let’s say that you are building a photo gallery & want the shortcode to show the photo gallery in the WordPress theme, so you can follow the code below:
[mygallery id="12345" size="large"]
Associated function for mygallery shortcode (goes in the functions.php file)
function mygallery_function( $atts )
$gallery_images = '<img src="flower.jpg" width="300" height="300" /><br />';
$gallery_images .= '<img src="apple.jpg" width="300" height="300" /><br />';
$gallery_images .= '<img src="liberty.jpg" width="300" height="300" />';
add_shortcode( 'mygallery ', 'mygallery_function' );
The above example is very simple & minimal. The usage can be extended & customized to our needs.
Simple, isn’t it?
Do you know of any other ways to use Shortcodes in WordPress themes? Feel free to suggest by commenting below.