Add Custom Sidebar To Post Categories

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.

Will do Christopher! I will start a new thread if it gets too long!

Thank you very much :slight_smile:

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.