Add Custom Sidebar To Post Categories

Hello support team,

I would like to add a custom sidebar to posts of different categories instead of having the main sidebar appear on every post.

I have seen that this is possible with adding code to your child theme but am unsure on which code to use.

Right now I would like to add the “EQ” sidebar to the “EQ” post category. I also plan to add more custom sidebars to different post types as well.

Any help would be appreciated!

Thanks!

Hi,

To achieve that you can add this in your child theme’s functions.php file

/* assign sidebar to support articles */
add_filter( 'ups_sidebar', 'add_eq_sidebar' );
function add_eq_sidebar($sidebar){
	if(is_category( 'eq' )){
		return 'ups-sidebar-eq';
	}
	return $sidebar;
}

You can add another conditional statement for other post types and sidebars

Hope that helps

Thanks for the code Paul!

Unfortunately it did not work! The “Main Sidebar” keeps showing up.

Did I do something wrong here?

I would like to add a custom sidebar to the posts with the category “EQ”

I would like to have the “EQ” sidebar appear on these EQ category posts

Help would be appreciated :smiley:

Hi there,

The Main Sidebar is for all posts and it will show on all posts including the EQ related categories. If you want to show ONLY few stuff in the EQ categories you need to add another Sidebar for NONEQ posts and add other stuff there and change the code in functions.php to:

/* assign sidebar to support articles */
add_filter( 'ups_sidebar', 'add_eq_sidebar' );
function add_eq_sidebar($sidebar){
	if(is_category( 'eq' )){
		return 'ups-sidebar-eq';
	} else {
              return 'ups-sidebar-noneq';
        }
	return $sidebar;
}

Thank you.

thank you for the update in code. It is working.

If I would like to add more post category specific sidebars, would I just copy the entire code over with the specific category and sidebar that I want?

Yes exactly, So the code will be like this:

/* assign sidebar to support articles */
add_filter( 'ups_sidebar', 'add_eq_sidebar' );
function add_eq_sidebar($sidebar){
	if(is_category( 'eq' )){
		return 'ups-sidebar-eq';
	} elseif ((is_category( 'somethingelse' )) {
		return 'ups-sidebar-somethingelse';
	} 
	 elseif ((is_category( 'anotherone' )) {
		return 'ups-sidebar-anotherone';
	} 
	else {
		return 'ups-sidebar-none';
	}
	return $sidebar;
}

You can add that chain of elseif statements to add more specific sidebars for the other categories. Thank you.

Thanks for the code. I have added it in but there seems to be a syntax error in it causing the site to not run

Here is the code that I have added and the syntax error


Any help would be appreciated!

Hey There,

There was a typo error on the code. Please use this is instead:

/* assign sidebar to support articles */
add_filter( 'ups_sidebar', 'add_eq_sidebar' );
function add_eq_sidebar($sidebar){
  if( is_category( 'eq' ) ){
    return 'ups-sidebar-eq';
  } else if ( is_category('somethingelse') ){
    return 'ups-sidebar-somethingelse';
  } else if ( is_category( 'anotherone' ) ) {
    return 'ups-sidebar-anotherone';
  } else {
    return 'ups-sidebar-none';
  }
  return $sidebar;
}

Please take note of the parenthesis.

Thanks for the update, unfortunately this still does not seem to be working

Hi There,

Could you please try using the following code and see if that helps.

add_filter( 'ups_sidebar', 'add_eq_sidebar' );
function add_eq_sidebar($sidebar){
	if (is_category ('eq') ){
		return 'ups-sidebar-eq';
		
	} elseif (is_category( 'somethingelse' )) {
		return 'ups-sidebar-somethingelse';
	} 
	 elseif (is_category( 'anotherone' )) {
		return 'ups-sidebar-anotherone';
	} 
	else {
		return 'ups-sidebar-none';
	}
	return $sidebar;
}

Make sure to clear your cache before testing.

Thanks!

Thanks for all the help guys but it is still not working.

Is there any other code that I can use to make this work?

Hi,

In your functions.php file, you have the following code in there.

function assign_bbpress_sidebar($sidebar){
  if ( x_is_bbpress() ){
    return 'bbp-sidebar';
  }
	
  return $sidebar;
}
add_filter( 'ups_sidebar', 'assign_bbpress_sidebar');

Please change it to

function assign_bbpress_sidebar($sidebar){
  if ( x_is_bbpress() ){
    return 'bbp-sidebar';
  }
	
	 if(is_singular('post')) {
        $category = get_the_category(); 

        if (isset($category[0]) && $category[0]->cat_name == 'EQ' ){
             return 'ups-sidebar-eq';        
         }elseif (isset($category[0]) && $category[0]->cat_name == 'Compressors' ){
             return 'ups-sidebar-compressor';        
         }
        elseif (isset($category[0]) && $category[0]->cat_name == 'Chorus' ){
             return 'ups-sidebar-chorus';        
         }
    }
	
  return $sidebar;
}
add_filter( 'ups_sidebar', 'assign_bbpress_sidebar');

Hope that helps

Thank you all for the help! This code is working now :grinning:

You’re most welcome!

Hey guys,

Unfortunately the code does not work when I try to add new sidebars to that are category specific.

The most recent code that Paul.r posted worked but when I added new categories for posts, it had this error message.

Any help adding in more category specific sidebars would be appreciated!

Hey There,

Your code is not working because you have invalid single quotes.

Please make sure that you are using the correct PHP quotes to avoid any errors.

Thank you for pointing out the errors! I appreciate it everything is working now!

Also, one quick question. If I wanted to have the same sidebar for multiple post categories, what would the code look like?

Thanks again for everyones help on this post!

  • Daniel

Hey Daniel,

You need to add another condition like this

elseif (isset($category[0]) && $category[0]->cat_name == 'Chorus' && $category[0]->cat_name == 'Compressors' )

Okay great!

Thanks for the help :slight_smile:

Glad that we could be of a help.

Kindly open up new threads for additional questions as it will help us to focus on each issue and give you a better support which you deserve. Having a long threads makes the maintaining job harder and also it will be harder for the other customers to find the correct information if they have similar issues. You are always welcomed to reply to this thread to follow up the same question.

Thank you for your understanding.