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

Adam Duffield
Adam Duffield
30,494 Points

How to add a sidebar to a category in wordpress?

Hi,

I just getting into wordpress development and my biggest issue is that I don't actually understand where to look when I want to make a change somewhere. At the moment the sidebar shows when i click on the blogpage or a single post but when I click on All Categories or any category the sidebar does not show. Any ideas which file I need to go into to edit? And then what I should put?

I'm guessing something along the lines of get('the_sidebar()'); or get_template_part('the_sidebar()');

Temp website is http://398.fa9.myftpupload.com/whats-new/

Many Thanks,

Adam

Stanley Thijssen
Stanley Thijssen
22,831 Points

You should be looking in the category.php file and add a sidebar with the following code:

<?php dynamic_sidebar( $index ); ?> 

Where $index is the name of the sidebar or the id of the sidebar.

Sue Dough
Sue Dough
35,800 Points

But he never registered it....

Stanley Thijssen
Stanley Thijssen
22,831 Points

The sidebar shows on his blogpage and single post template so he already registered the sidebar else it wouldnt be working on those pages aswell.

He just has to edit his category.php file.

Sue Dough
Sue Dough
35,800 Points

Well he would still have to register it since it sounds like its another sidebar.

2 Answers

Sue Dough
Sue Dough
35,800 Points

You need to register your sidebar in functions.php and then call it in the appropriate template file. Calling it first without registering as previously mentioned would simply not work.

http://www.wpbeginner.com/wp-themes/how-to-add-dynamic-widget-ready-sidebars-in-wordpress/ https://codex.wordpress.org/Function_Reference/register_sidebar

Look at these references. Post your code if your having trouble.

Adam Duffield
Adam Duffield
30,494 Points

Thanks for the replies guys, I haven't had the chance to try your code yet because the main problem is that I can't find category.php, I'm using Brooklyn themes and have checked through fileZilla and can't find the file anywhere. If i actually knew where to put my code I reckon I might have been able to find the answer myself. Are there any other common names for category.php? or does it have to be called category.php for the file to work as a category with wordpress?

Stanley Thijssen
Stanley Thijssen
22,831 Points

category.php is the main file used for your category pages. You can check out the theme hierarchy here to see what template your category page is using.

http://wphierarchy.com/

I think the best thing you can do is copy the code from your blogpage where the sidebar shows and paste it in a new file called category.php and adjust the code to your needs.

Just take a look at the template hierarchy to see what pages use what templates. Then it should be a piece of cake!