Putting It All Together

Template files can't do a whole lot by themselves. The real power comes when they're put together.

Connecting the templates

WordPress has built-in functions to include the main template files, such as header.php, sidebar.php, and footer.php, in other templates. An include function is a custom PHP function that is built in to WordPress, allowing you to retrieve the content of another template file and display it along with the content of another template file. Table 3-5 shows the templates and the function to include them.

Table 3-5 Template Files and Include Functions

Template Name Include Function
header.php <?php get_header(); ?>
sidebar.php <?php get_sidebar(); ?>
footer.php <?php get_footer(); ?>
search.php <?php get_search_form(); ?>
comments.php <?php comments_template(); ?>

If you want to include a file that doesn't have a built-in include function, you need a different piece of code. For instance, if you want to add a unique sidebar to a certain page template, you could name the sidebar file sidebar_page.php. To include that in another template, you would use the following code:

<?php get_template_part('sidebar_page.php'); ?>

In this statement, the PHP get_template_part function looks through the main theme folder for the sidebar_page.php file and displays the sidebar.

In this section, you put together the guts of a basic Main Index template by using the information on templates and tags we provide so far in this chapter. There ...

Get WordPress® All-in-One For Dummies® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.