wordpress

WordPress Quick Tip: Using Shortcodes in Templates

A WordPress project I’m currently working on presented me with a situation that I soon realized was calling for a custom WordPress shortcode. In a future post I’ll cover the process of creating a custom shortcode, but for today I’ll share a quick tip on how to use shortcodes in your theme templates as opposed to a post’s content. 

About Shortcodes

Shortcodes are a very powerful and effective aspect of WordPress, usually used for activating code functions from within a post’s content in a quick and easy way. These functions can be a part of the WP core, from a plugin, or from a custom shortcode provided by a theme. The simplest application of a shortcode in a post’s content looks like this:

[shortcodename]

Their usage can also be more involved with numerous variables passed to the function via attributes:

[shortcodename attribute1=”value1″ attribute2=”value2″]

Shortcodes in Template Files

So how do we apply these same shortcodes within a theme’s php template file? Easy! WordPress provides us with the solution in the form of the do_shortcode() function. Here’s what it looks like:

<?php echo do_shortcode('[shortcodename attribute1="value1" attribute2="value2"]'); ?>

And there you have it! You were expecting there to be more to it? Sorry to disappoint! 😉

 

One thought on “WordPress Quick Tip: Using Shortcodes in Templates

Leave a Reply

Your email address will not be published. Required fields are marked *