Assigning different sidebars for each post category

Hello support team.

On my website, all posts are categorized as either Press or Thought Leadership. I do not use sidebars on the main blog page or category archive pages. However, I want each individual post page to have a sidebar. Specifically, I want posts that are categorized as Press to have one sidebar and posts that are categorized as Thought Leadership to have a different sidebar. Accordingly, I have created one sidebar called Press and another sidebar called Thought Leadership.

I tried simply assigning by taxonomy, but that didn’t work. As discussed elsewhere, I think that function was designed for the main blog and/or archive pages only.

So then I tried to follow the instructions found at this thread, whose author had a similar need, but I was not successful.

This is what I added to functions.php in my child theme:

/* assign sidebar to support articles */

add_filter( 'ups_sidebar', 'add_press_sidebar' );
function add_press_sidebar($sidebar){
    if (is_category ('press') ){
        return 'ups-sidebar-press';
	
    } elseif (is_category( 'thought-leadership' )) {
        return 'ups-sidebar-thought-leadership';
    } 
    else {
    	return 'ups-sidebar-none';
    }
    return $sidebar;
}

The behavior I’m seeing is that the Thought Leadership sidebar shows up on posts of both categories.

Hi Leslie,

Thanks for writing in!

To make sure that the code will work for you, please make sure that you have named your sidebar correctly. For example, in the sidebar settings, you have this:

We would love to know if this has worked for you. Thank you.

Yes, I think they were named correctly. See screeenshots here:


Any other suggestions? How should these two sidebars be assigned/not assigned to pages/posts for the PHP I added to functions.php to work correctly, or does it matter?

Hello Leslie,

Would you mind providing us access to your site so that we can check your settings? Please create a secure note with the following info:
– Link to your site
– WordPress Admin username / password

To know how to create a secure note, please check this out: https://theme.co/apex/forum/t/how-to-get-support/288

Best Regards.

Hello Leslie,

I have modified your code and use this:

/* assign sidebar to support articles */

add_filter( 'ups_sidebar', 'add_press_sidebar', 10, 3 );
function add_press_sidebar($sidebar){
	if (in_category ( 'press' ) ){
		return 'ups-sidebar-press';
	} else if (in_category( 'thought-leadership' )) {
		return 'ups-sidebar-thought-leadership';
	} else {
		return 'ups-sidebar-none';
	}
	return $sidebar;
}

is_category() is for archive pages. in_category() will check if the post is assigned to a particular category.

Please check your site now.

Brilliant! It’s fixed, and your explanation makes perfect sense. Thank you.

You’re welcome!
We’re glad we were able to help you out.

I have a main blog which uses the blog sidebar and I also have categories (journals, newspapers, radio) on another page (using recent posts) with uses another sidebar called media-reports. Would you please give help me with the code to add to the functions.php file? I tried following the solution offered here and an earlier page but failed. Thank you!

Hi Cindy,

Thank you for reaching out to us. Please make sure you’re using the correct names of the Sidebar, e.g to display a custom sidebar for journals category, add a new sidebar and name it journals:

The ID of your journals sidebar should look like this:

Now get the slug name of your category by navigating to WP Admin > Posts > Categories e.g for the Audio category you can find it’s slug here:

Then you can add the following code in your child theme’s functions/php file:

/* assign sidebar to support articles */

add_filter( 'ups_sidebar', 'add_press_sidebar', 10, 3 );
function add_press_sidebar($sidebar){
	if (in_category ( 'journals' ) ){
		return 'ups-sidebar-journals';
	} else if (in_category( 'newspapers' )) {
		return 'ups-sidebar-newspapers';
	} else if (in_category( 'radio' )) {
		return 'ups-sidebar-radio';
	} else {
		return 'ups-sidebar-none';
	}
	return $sidebar;
}

If you still have problems then please create a new thread with the URL/User/Pass of your WordPress dashboard using the Secure Note functionality of the post to follow up the case.

Thanks!

Thank you. But while the second sidebar is working fine for the 3 categories, the main blog sidebar won’t appear. Please let me know what went wrong. Thanks.

Hey Cindy,

It’s because of this line in the code:

It should be return 'sidebar-main'; or any custom sidebar of your choosing.

Please remember to not post your credentials to other user’s thread next time as they could see all the secure notes in their thread. Please open a new thread and just link the related thread for reference.

Hope that helps.

THANK YOU, and appreciate the tip about credentials, will delete.

You are most welcome. :slight_smile:

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