Custom WordPress includes

Anyone who has ever spent any time developing a WordPress theme will know the following tags:

<?php get_header(); ?>
<?php get_footer(); ?>
<?php get_sidebar(); ?>

Their purpose is fairly obvious. They are grabbing the header, footer and sidebar and including them in our templates. But what happens if you want to include something else? Well, there are two ways to do this. The first is a standard php include:

<?php include('custom.php'); ?>

That works fine but it isn’t in keeping with the other WordPress tags that you’ll be using and therefore may cause confusion as the project develops. The alternative is as follows:

<?php get_template_part('custom'); ?>

That way, as long as the file you want to include is called custom.php then it will grab whatever is in that file and include it in your template. Obviously you would need to change ‘custom’ to whatever your file name is but as you can see this is very straight forward.

As far as I know this is only available in WordPress 3.0 onwards but I know that I’ll be using it a lot to make my future themes more manageable.

You can be more specific than this with WordPress includes too. Take the get_sidebar tag for instance. You might have a different sidebar for every page of your site. An easy way to achieve this is to add your file name in-between the brackets of the get_sidebar tag like so:

<?php get_sidebar('contact_sidebar'); ?>

You can also do this with the get_header and get_footer tags which is certainly a useful little feature.

I realise for some this may all be old news but this is something I only discovered recently and it has already changed the way I put WordPress themes together. As the old saying goes, ‘you learn something new every day’.