This is a great plug-in. We have a widget area to display certain widgets depending on what category is being shown. It works on category archive pages with no problem. But on single posts within a specified category, they do not work.
I found this forum topic at https://dfactory.co/support/topic/restrict-widgets-to-specific-posts/ where you show the user how to use the following code to modify so that subcategories will display widgets if they meet the proper code….
function custom_rw_display_widget ( $display, $instance ) {
// break if we're not on single post
if ( ! is_single() )
return $display;
// get current post id
$post_id = get_the_ID();
if ( $instance['title'] == 'Paris offers' ) {
// let's check if current post is assigned to a Paris subcategory in Citytrip category
// 1 is an ID of Paris
if ( has_term( 1, 'category', $post_id ) ) {
$display = true;
}
} elseif ( $instance['title'] == 'Berlin offers' ) {
// 2 is an ID of Berlin subcategory in Citytrip category
if ( has_term( 2, 'category', $post_id ) ) {
$display = true;
}
}
// and so on
return $display;
}
add_filter( 'rw_display_widget', 'custom_rw_display_widget', 10, 2 );
I tried it by substituting two subcategory id numbers in the two slots above, but no widgets showed up on the single posts whose categories where those subcategories. Your code and explanation imply that we should be able to see them. Is there something I’m missing here? Would appreciate your advice.