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

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Using get_sidebar() twice in a template

Hi all,

I'm developing a WordPress theme for my website. I have my widget area set up for my blog page. But as it can be quite lengthy I want to have it in 2 places. At the top and the bottom of the loop.

Here's a basic outline.

<?php get_sidebar(); ?>

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

                <h1><?php the_title();  ?></h1>
                <?php the_content();  ?>

            <?php endwhile; else : ?>

                 <!-- -->
                 <p>Sorry, found no content.</p>

            <?php endif; ?>


<?php get_sidebar(); ?>

If I try to use the get_sidebar function like this, only one at the top displays.

I suspect I need to set up a new area and possibly a new template with one area being the primary, and the secondary. I've looked and can't find any confirmation of this but can the get_sidebar() only be used once?

Thanks :)

1 Answer

Will they have the same content or will they be different? The function that loads sidebar does some protection to prevent multiple calls to it but you can call it a second time doing this:

<?php include( TEMPLATEPATH . '/sidebar.php'); ?>

This method directly calls the sidebar.php template part instead of using the function. If you need more than one sidebar and they will be different, you can then just call their name in the get_sidebar() function like so:

<?php

get_sidebar('secondary');
?>
Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Yes it'll be the same content.

I'll try those tips thanks. It may be worth just setting up a second area with a secondary template but i wanted to avoid that as it's simply going to be a second copy of the same widgets :-)

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Just tested your solution and it worked perfectly, thanks :-)

Awesome, glad it helped you!