Automatically Assign Custom Sidebars To Posts By Categories

My website has three “sub-sections” (El Paso / Monterey / New Mexico) each one defined by its own “Category.”
Each sub-section needs to display their own Sidebar.

I have been able to make the correct Sidebar Automatically appear on the correct “Pages” in each sub-section, but I cannot make the correct Sidebar appear on the “POSTS” that go with each sub-section.
I know that I can assign the sidebar manually, post by post, but I need for it to be automatic once the post has been assigned a Category.

I have tried this piece of code found the Apex Forum:

but I cannot get it to function on my site.
I suspect that there are elements of this code example that are specific to the site in question, and that will not work on mine.

Can you send me a Code Snippet that is “neutral,” and ready for my site-specific information.

I am running WordPress 4.9.4
and the Integrity Stack

Here is the URL for my Developer Site:
http://dev-guestlife.pantheonsite.io/
What other information do you require?

Best Regards.

Hey There,

Thanks for writing in!

The code may not work because it does not have the correct category names and the sidebar slug or sidebar ID. Please ensure that you have the correct one taken from your site:

function assign_custom_sidebar($sidebar){

	if (is_singular('post')) {
		$category = get_the_category(); 

		if (isset($category[0]) && $category[0]->cat_name == 'CategoryA' ){
			return 'ups-sidebar-category-a';        
		} elseif (isset($category[0]) && $category[0]->cat_name == 'CategoryB' ){
			return 'ups-sidebar-category-b';        
		} elseif (isset($category[0]) && $category[0]->cat_name == 'CategoryC' ){
			return 'ups-sidebar-category-c';        
		}
	}
	
  return $sidebar;
}
add_filter( 'ups_sidebar', 'assign_custom_sidebar');

And to find the sidebar slug or sidebar ID, please go to Appearance > Sidebars and edit one of the sidebars.

To get the the category name, you ca simply go to Posts > Categories.

Hope this helps.

Thank you for the quick response.

The supplied code is “almost” working. Here is what I’m using:

function assign_custom_sidebar($sidebar){
if (is_singular(‘post’)) {
$category = get_the_category();
if (isset($category[0]) && $category[0]->cat_name == ‘Monterey Bay’ ){
return ‘ups-sidebar-monterey’;
} elseif (isset($category[0]) && $category[0]->cat_name == ‘El Paso’ ){
return ‘ups-sidebar-el-paso’;
} elseif (isset($category[0]) && $category[0]->cat_name == ‘New Mexico’ ){
return ‘ups-sidebar-new-mexico’;
}
}
return $sidebar;
}
add_filter( ‘ups_sidebar’, ‘assign_custom_sidebar’);

Here is a shortened list of my Categories:

NOTE that I will be using two additional “parent” categories of “El Paso” and “New Mexico” - each of these will have their own list of children.
It is these three categories: Montery Bay, El Paseo, New Mexico, that I am trying to target with their own sidebars.

While I cannot get “Monterey Bay” to work as the targeted Category Name,
I CAN get some of the Child-Categories of Monterey Bay to work (“Area, Attractions”)

It seems that the problem is in the name of the Category - are ONLY single words with no spaces allowed?
Or is there another Naming Syntax that I’m missing?

I have just found a way to force this to work.

If I put an Underscore ( “_” ) at the beginning of the Category that I want to target ( _Monterey Bay, _El Paso, _New Mexico) making the Parent-Category alphabetically superior to the Child-Category, it works correctly.

I noticed that if the name of the Child-Category was alphabetically BEFORE the name of the Parent-Category the code would not put the sidebar on the page.

BUT, if the Child-Category name was alphabetically AFTER the Parent-Category the code worked.

Attractions / Montery Bay = no
_Montery Bay / Attraction = YES

So, I have forced it to work.

Now my question is: is there a BETTER way to make this function correctly?
I have tried the “Make Primary” function in the Edit Post window, but it doesn’t appear to do what I want.

Hi,

The code provided above is for when post has single category only. To make it get all categories please change code to this.

  function assign_custom_sidebar($sidebar){

    if (is_singular('post')) {
        $categories = get_the_category(); 
        $mycat = array();
        foreach($categories as $category) {
            $mycat[] =  $category->cat_name;
        }

        if (in_array("CategoryA",$mycat) ){
            return 'ups-sidebar-category-a';        
        } elseif (in_array("CategoryB",$mycat)){
            return 'ups-sidebar-category-b';        
        } elseif (in_array("CategoryC",$mycat) ){
            return 'ups-sidebar-category-c';        
        }
    }
    
  return $sidebar;
}
add_filter( 'ups_sidebar', 'assign_custom_sidebar');

Hope that helps

Brilliant - working perfectly!
Thanks so very much for your help. :clap:

You are always welcome in behalf of the team!

Glad this works for you.

Cheers!

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