Thord Daniel Hedengren - Smashing WordPress_ Beyond the Blog-Wiley (2014)

(avery) #1

32 PART I • Getting Started with WordPress


The include tags differ from other template tags in that what you’re doing is including other
files, rather than adding a specific type of functionality or element. In other words, the include
tags include the template files that contain the template tags.

You’ll find them in your theme’s index.php file, for example, and they automatically include
header.php, footer.php, and sidebar.php, respectively, where the tags are placed. These three
tags actually support alternative headers, footers, and sidebars by adding a string to the tag,
like this:

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

This would include sidebar-left.php rather than the default sidebar.php, so you’d need to
create that file, of course. Should sidebar-left.php not be present, get_sidebar() will
revert to sidebar.php. The same principle goes for get_header() and get_footer().

You might also find the get_template_part() include tag, fetching the loop from its
own template file, like so:

<?php get_template_part( 'loop', 'index' ); ?>

The first parameter, loop, tells WordPress that it is supposed to get a loop template. The
second parameter tells WordPress to look for a loop template called loop-index.php. If the
second parameter had been page instead of index, WordPress would have looked for
loop-page.php instead. Should loop-index.php not be found, WordPress will default to loop.
php. I’ll dig into this in Chapter 3. The same logic will work for other parameters as well, so it
doesn’t have to be loop.

If you want to include other files such as an alternative template for your content, for example,
you can use get_template_part() for that as well:

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

Just remember that get_template_part() is meant for including template parts —
nothing else.

Finally, there’s an include tag for the comments, which any good theme has in comments.php.
Should there be no comments.php file, WordPress will include one from wp-includes/
theme-compat/comments.php instead, which is a fallback file that the system provides. Just
put the comments_template() tag where you want comment functionality and remove it
where you don’t think you need it:

<?php comments_template(); ?>

If you want to, you can load an alternative file for comments by passing a parameter to
comments_template(). You can also choose to separate comments by type (true or false),
which would mean that regular comments and pingbacks would be separated if set to true.
You can read more about comments_template() in the Codex at http://codex.
wordpress.org/Function_Reference/comments_template.
Free download pdf