Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Darren Healy
seal-mask
.a{fill-rule:evenodd;}techdegree
Darren Healy
Front End Web Development Techdegree Student 23,565 Points

How to add and use different sidebars for different pages in Wordpress?

I want to be able to use a different sidebar for a different page on my Bootstrap to Wordpress blog site.

Basically I am happy displaying the main sidebar on nearly all of my pages but one; the contact page.

I would like for the sidebar on the contact page to display a sidebar with it's own unique content.

Now, I have tried to tinker with it a bit, but I can't seem to get it to work.

Here's what I have done so far:

1) Added this to my functions.php file:

create_widget( 'Contact Sidebar', 'contact', 'Displays on the side of contact pages with a sidebar' );

2) Created a new contact.php file to act as a new page template selectable in the admin area:

<?php
/*
  Template Name: Contact Page with Sidebar
*/

?>
<?php get_header(); ?>

  <div class="container">   
    <div class="row">

      <div class="col-md-9">

        <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

          <div class="page-header">
            <h1><?php the_title(); ?></h1>
          </div>

          <?php the_content(); ?>

        <?php endwhile; else: ?>

          <div class="page-header">
            <h1>Oh no!</h1>
          </div>

          <p>No content is appearing for this page!</p>

        <?php endif; ?>


      </div>

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

    </div>

<?php get_footer(); ?>

3) I created a contact-sidebar.php file (but I'm not sure why, I just followed the procedure before when creating a sidebar for the blog)

<div class="col-md-3 sidebar">

    <?php if ( ! dynamic_sidebar( 'contact' ) ): ?>

    <h3>Sidebar Setup</h3>
    <p>Please add widgets to the contact sidebar to have them display here.</p> 

    <?php endif; ?>

</div>

4) Added my page in the admin area, used the template, BUT it loads up the page sidebar:

create_widget( 'Page Sidebar', 'page', 'Displays on the side of pages with a sidebar' );

Zac Gordon I know you are lurking around the forum just now... :)

2 Answers

Darren Healy
seal-mask
.a{fill-rule:evenodd;}techdegree
Darren Healy
Front End Web Development Techdegree Student 23,565 Points

False alarm, the problem was the name of my file. I had called it contact-sidebar.php, when it should have been sidebar-contact.php.

Thanks again Zac you legend.

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher
get_sidebar('custom'); 

Will load sidebar-custom.php.