Using WordPress Shortcode In Templates
Did you know you can use WordPress shortcode outside of the loop? You can actually add shortcode directly to your theme's templates. It's easy to pull off, but you just need to know what to do. Let's have a look!
If you're a frequent visitor to Ten Ton, you've no doubt noticed the changes that have been happening on the site. We've been streamlining our layouts, adding in some social media buttons, and simplifying the navigation. During this process, I discovered a neat little WordPress hack for sticking shortcode outside the WordPress loop, dropping it directly in template files.
If you're a WordPress user, you know that shortcodes are used as a fast way for inserting things like media files, social media buttons, and other types of content into posts quickly without a lot of hand coding. What I wanted to do though was add content, via shortcode, to every single post on the site. The typical way of doing this would have been to paste in the shortcode into each and every post, one at a time. But, this would have added the shortcode within the loop. Not what I wanted.
I got thinking about an alternative…adding the shortcode directly to the PHP template pages.
It's actually really easy to pull off. You have your shortcode handy, square brackets 'n all? Okay, open the template page that you want to add the shortcode to. For me, it was single.php because I wanted my shortcode added to every post, right?
Within the template file, find the spot where you'd like to add the shortcode, then do this:
Be sure to replace [SHORTCODE] with your actual WordPress shortcode, and you're good to go. Save your template file and go check out your results.
Pretty cool, huh?
Related posts:Pre-Order Ten Ton WordPress Essentials!
Ten Ton WordPress Essentials DVD Sample Two!
Ten Ton WordPress Essentials DVD Sample One!
Geoff Blake's Blog
- Geoff Blake's profile
- 3 followers
