Although WordPress has one of the more forgiving learning curves, there are a number of code snippets that can really transform your themeing development to a whole new level.
You can get away with plenty of hack-ups and your theme will work, but if you throw it around much at all, it’ll break.
Here are a few WordPress code snippets to know and love:
1. Template Directory
This is the most important snippet you should know when developing WordPress themes.
This will return an absolute URL to the current theme directory, so when you pull an image or call a script, it’s going to be pointing in the right place, no matter where you are using your theme.
Code
[cc lang=”php”][/cc]
Example
[cc lang=”php”]/js/jquery.js" type="text/javascript">[/cc]
This will make your transition from your local dev environment to going live a breeze.
2. Custom Query
This snippet is silent, but deadly. It's the WordPress theme developers secret weapon. After successfully pulling-off a few of these, you'll feel pretty awesome. I promise.
Before I learned about this gem, I relied on plugins and widgets to do this:
You might use this for querying featured posts to render an image slideshow, or to simply display a list of news posts on a page or in the sidebar, as an alternative to widget functionality (as it’s more flexible).
Code
[cc lang="php"]
have_posts()) : ?>have_posts()) : $query->the_post(); ?>
[/cc]
Example
[cc lang="php"]
have_posts()) : ?>
[/cc]
Check the WordPress Codex for more info.
3. If Have Posts, While Have Posts, The Post
This is used all over your template files.
It is the beginning of The Loop, and is needed to fetch content from your WordPress database.
Code
[cc lang="php"][/cc]
Example
[cc lang="php"]
Sorry, no posts found.
[/cc]
Essential junk.
4. The Permalink
Like the Template Directory snippet, this will make your WordPress theme stronger, thus, harder to break.
Used inside The Loop to process Posts or Pages, the link will output based on your defined permalink structure. That way, the structure can change, and won't bust your theme.
Code
[cc lang="php"][/cc]
Example
Simple, but essential.
5. Custom Field Value
Custom Field Value's are so fancy.
Code
[cc lang="php"]ID, 'custom-field-key', true); ?>[/cc]
Example
One of my more recent uses for custom fields was on a client’s website that had a library of information about guitar effects pedals. The owner of said website affiliated with a few different online stores – and gets a commission if one of these effects pedals sells should a customer buy one after following a link from their website. The referral links worked so that my client could link directly to the product on one of the affiliate websites, and simply put some referrer information at the end of the URL.
I had set up a custom post type for the effects pedals, so using a custom field seemed like a simple step forward to achieve an easy way for the client to manage this data. The custom field key was “affiliate” and they added the link in the value box. On the template, the custom field was used to output a “Buy Now!” button.
[cc lang="php"]Buy Now![/cc]
Mmmmm. The power of WordPress.
More, tomorrow!
Drop any of your favorite snippets off in the comments!
[…] 5 More WordPress Code Snippets to Love Posted By Eric Dye on August 9, 2011Yesterday, I covered five WordPress Code snippets to love and I hope you love these, today, just as much.These aren’t the best 10 WordPress code […]