Free Wordpress Blog Setup

Easily Add Custom Header, Footer, or Sidebar for Each Category

By Editorial Staff in Themes
Easily Add Custom Header, Footer, or Sidebar for Each Category

Did you ever see WordPress blogs that have a separate header for different categories? In this tutorial we will share a simple snippet that you can use to add custom header, footer, or even sidebar for each WordPress category.

To call a particular header, you will need to open your index.php and replace your normal header code with this one:

<?php if (is_category('Blogging')) {
get_header('blogging');
} else {
get_header();
} ?>

This code above is basically telling WordPress that if someone opens the category called “Blogging” then WordPress needs to display a file called header-blogging.php if it exist. If it does not exist, or if the category is not blogging, then WordPress displays the default header file.

To get a separate sidebar for each all you need to do is add the following code:

<?php if (is_category('Blogging')) {
get_sidebar('blogging');
} else {
get_sidebar();
} ?>

The code above will look for sidebar-blogging.php to replace the default footer file if the category is Blogging.

To get a separate footer for each category all you need to do is add the following code:

<?php if (is_category('Blogging')) {
get_footer('blogging');
} else {
get_footer();
} ?>

The code above will look for footer-blogging.php to replace the default footer file if the category is Blogging.

What Next?

Digg it
Save This Page
Subscribe to WPBeginner
Stumble it
Free Wordpress Blog Setup

Comments

10 Responses to “Easily Add Custom Header, Footer, or Sidebar for Each Category”
  1. Jason Pelker says:

    Is there a way to change the sidebar/header on a single post, depending on the post’s category?

  2. wrerm says:

    I recently purchased a WP Book and designed a wesbsite using WP and this is very useful. Thanks!

  3. ohhh this is what i have been looking for!!! Have been trying to figure out how to display a section with advertisements related to categories in wordpress and here it is!!!

    thank you HEAPS!

  4. shravan says:

    Thank you so much for this post. This really bailed me out of big trouble. I looked for plugins to do this job for me but couldn’t find any. I was so glad to find the solution on this post.
    Now I can create different headers for different categories.
    Thanks again and keep up the good work.

  5. sven says:

    hi, thanks for a a great tutorial, this is just what i was looking for. i now have a custom sidebar for one of my categories, and as I kept the dynamic sidebar code I thought I would be able to add widgets to it. It however does not show up in the widget editor. how do i get it so I can add widgets to my category specific sidebar?

  6. Ljuboja says:

    Does this trick do the same with pages?
    Tnx !

  7. DaveK says:

    WPBeginner does it again! I was looking for ages for a solution to add a different sidebar to different categories, and this worked a treat. Thanks

Share Your Opinions

Tell us what you're thinking...
and if you want a pic to show with your comment, then get gravatar!

Please make sure that you have read our Comment Policy.

Due to high volume of request from our readers, we are adding this feature that allows you to stay updated with this post's comments without having to participate in the discussion even though we would love your input as always. Don't worry we hate SPAM just as much as you do, so you will never receive any SPAM messages from our site and that's our promise to you.

Subscribe without commenting

Close Bar