Support » Plugins » Cutomizing or Removing the sidebar depending on wordpress categories?

  • Resolved shafee85



    I am creating a website where there are multiple amounts and levels of categories. I would want to have the ability to customize the sidebar (either by not displaying the sidebar in certain categories, or changing its content depending on there categories)

    So my questions are,
    1. How do i not display the sidebar depending on a certain category.
    2. How do i change content of the sidebar depending on a certain category.

    Would really appreciate any help as i am not very strong with PHP. i know that for the 2nd question i will need to create a file called sidebar2.php. but how do i link that to the main files?

    Thanks for reading.

Viewing 6 replies - 1 through 6 (of 6 total)
  • You first need to understand the Template Hierarchy and Category Templates.

    If you create a Category Template for your specific categories, then in the category template, say for category ID 2, you could just use:

    get_sidebar('cat2'); // will include sidebar-cat2.php

    Thank you very much for the reply. The links helped me understand the workings around themes quite a bit better.

    The code you quoted works perfectly if it is for a sigle category. I applied it to multiple categories but it doesn’t work correctly.

    <?php if (is_category('7'||'8'))

    Yes the code applies to category 7 and 8, but it also applies to other categories as well.

    What am i doing wrong? Im pretty weak in coding unfortunately.

    Ok i found out the error of my above code. The code should read as follows,

    if (is_category('7') || is_category('8'))

    Thanks MichaelH! you rock!

    I’ve got another question which is related to this thread. Is there anyway i can give the parent category id and make that automatically apply to all the child categories underneath it?

    Taking the above example into account, if i apply the above if condition to category id 7, is there a way to apply the same if condition to all the children of category id 7?

    This will enable the code to be more dynamic and help me to stop changing the category id each time i need to change them.

    Hope there is a solution for this.

    From some note:

    // in category template, test if queried category is child descendant of another category
    if ( is_category() ) {
    $parent = 7;
    $categories = get_categories('include='.get_query_var('cat'));
    //echo "<pre>"; print_r($categories); echo "</pre>";
    if ( $categories[0]->category_parent == $parent ) {
    echo 'category ' . $categories[0]->name . ' is a child of category ' . $parent;

    Thanks for the help again. 🙂

    I am trying to do this as well, but I cannot seem to implement this into my theme?!

    I would like this page to not have a sidebar as well as everything else in the loop. Any assistance please!? Thank you!!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Cutomizing or Removing the sidebar depending on wordpress categories?’ is closed to new replies.