Sidebar Management

Hi, we have a category called “forehand-drills” and created a special sidebar called “forehand-drills” and a Widget with the same name. Now i want to show this sidebar only to posts of that category. Do i have to put code into the child functions php? Marking the taxonomy in the usp management doesnt work. Only if i mark a special post. But every category has about 50 posts. Marking all of them is not an option.

thx for helping.

Hey @thegator,

Regretfully, there’s no option for that yet so yes, you’ll need to use custom code. Here’s a template code to assign a sidebar to a post or any page that displays the sidebar. The code must be added in functions.php of your child theme. Just add your own conditions and sidebar ID. The code sample below applies for Single Posts in Audio category.

add_filter( 'ups_sidebar', 'assign_sidebar');

function assign_sidebar( $sidebar ){ 
       if ( is_singular( 'post' ) && in_category( 'audio' ) )  {      
           return 'ups-sidebar-lorem'; // Sidebar ID
       }    
  return $sidebar;
}

Hope that helps.

Hi, that worked perfect, Is there a way to put all 30 categories in that code or do i have to put in the code 30 times and replace the Categories?

LIKE THIS EXAMPLE

add_filter( ‘ups_sidebar’, ‘assign_sidebar’);

function assign_sidebar( $sidebar ){
if ( is_singular( ‘post’ ) && in_category( ‘cat1’ ) ) {
return ‘ups-sidebar-cat1’; // Sidebar ID
}
return $sidebar;
}

function assign_sidebar( $sidebar ){
if ( is_singular( ‘post’ ) && in_category( ‘cat2’ ) ) {
return ‘ups-sidebar-cat2’; // Sidebar ID
}
return $sidebar;
}

Hello @thegator,

You can use if else statement in the code to have more categories in it:

add_filter( 'ups_sidebar', 'assign_sidebar');

function assign_sidebar( $sidebar ){ 
  if ( is_singular( 'post' ) && in_category( 'audio' ) )  {      
     return 'ups-sidebar-lorem-1'; // Sidebar ID
  } elseif if ( is_singular( 'post' ) && in_category( 'video' ) )  {      
     return 'ups-sidebar-lorem-2'; // Sidebar ID
  } else {
     return 'ups-sidebar-lorem'; // Sidebar ID
  }  
  
  return $sidebar;
}

Hope this helps.

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