WordPress For Dummies, 8th Edition
Book image
Explore Book Buy On Amazon
You can create separate sidebar templates for different pages of your WordPress site by using an include statement. When you write an include statement, you’re simply telling WordPress that you want it to include a specific file on a specific page.

The code that pulls the usual Sidebar template (sidebar.php) into all the other templates, such as the Main Index template (index.php), looks like this:

<?php get_sidebar(); ?>

What if you create a page and want to use a sidebar that has different information from what you have in the Sidebar template (sidebar.php)? Follow these steps:

  1. Create a new sidebar template in a text editor such as Notepad or Text Edit.

    You might want to make a copy of the existing sidebar.php file in the Twenty Sixteen theme and rename it.

  2. Save the file as sidebar2.php.In Notepad, choose File → Save.

    When you’re asked to name the file, type sidebar2.php and then click Save.

  3. Upload sidebar2.php to your Themes folder on your web server.

    The template is now in your list of theme files on the Edit Themes screen. (Log in to your WordPress Dashboard and click the Editor link on the Appearance menu.)

  4. To include the sidebar2.php template in one of your page templates, replace <?php get_sidebar(); /> with this code:

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

    This code calls in a template you’ve created within your theme.

By using that get_template_part function, you can include virtually any file in any of your WordPress templates. You can use this method to create footer templates for pages on your site, for example. First, create a new template that has the filename footer2.php. Then locate the following code in your template:

<?php get_footer(); ?>

and replace it with this code:

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

About This Article

This article is from the book:

About the book author:

Lisa Sabin-Wilson is cofounder of WebDevStudios, one of the largest WordPress design and development agencies in the world. She is a regular public speaker at national events on topics such as WordPress, development, design, CSS, and social media.

This article can be found in the category: