Tips and Tricks

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.

Shortcode:

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:

Example 1:

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:

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).

Example 2:

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:

Associated function for mygallery shortcode (goes in the functions.php file)

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.

2 Comments on WordPress Shortcodes: How to use Shortcodes in WordPress Theme Examples

  1. 1

    I am using plugin like to read http://wordpress.org/extend/plugins/like-to-keep-reading/
    problem is its using opening and closing shortcodes,when I embed them to my site http://vijaykudal.info only opening short codes displayed,can you please help me?
    PS:-tried with different themes as well