A brief chat with Matthew just highlighted something that might not be obvious to all WordPress users: why you want to keep theme pieces generic and reusable. WordPress has a pile of functions baked in to make it easy for you to get content out of your loops without having to do a lot of programming.
Let’s use an example: linking your site’s name to the frontpage of your website. Of course this is something you’re already familiar with, just making a simple link to your domain name.
It’s as easy as writing
<a href='http://my-awesome-website.com'><h1> My Awesome Website </h1> </a>
All we’ve done here is made our site name an H1 and linked it to our domain name. Paste that into the right spot in header.php
and we’re done for the evening.
But there’s a better way. Next time, when you’re coding a site for a friend or client, you’ll need to change http://my-awesome-website.com
to http://another-mindless-website-for-corporate-america.com
manually. It’s not a big deal, but every second counts these days. Instead of baking the site name right into the header template file, use the WordPress function bloginfo()
for guaranteed reusability. Like so:
This will grab the name of your site:
<h1><?php bloginfo('name'); ?></h1>
This will get the url of the homepage:
<?php php bloginfo('url'); ?>
And finally, this will pull it all together and make the link work:
<a href="<?php bloginfo('url'); ?>" title="<?php bloginfo('name'); ?>"><?php bloginfo('name'); ?></a>
All that goodness came from just one single function, bloginfo()
.
Starting a new website or theme is a pain. You need to go through and setup the site name, the url, the contact info, etc, etc. If you use the built-in WordPress functions in your themes you’ll save time and frustration down the road. Keeping as much of your themes as generic as possible will make them more reusable and make that first hour with a new theme a lot less of a chore.
Ps: There’s a lot of other handy functions available for you to use. WordPress calls them Template Tags. Happy theming.