Hi and thank you.
THe code snippet you posted can handle that. You’d have to create multiple conditions like this:
function custom_rw_display_widget ( $display, $instance ) {
if ( $instance['title'] == 'Paris offers' ) { // your Paris offer widget title
if ( is_single( array( 1, 2, 3 ) ) ) { // ann array of post ids related to Paris
$display = true; // or false if you want to hide it
}
} elseif ( $instance['title'] == 'Berlin offers' ) {
if ( is_single( array( 4, 5, 6 ) ) ) { // an array of post ids related to Berlin
$display = true; // or false if you want to hide it
}
}
// and so on
return $display;
}
add_filter( 'rw_display_widget', 'custom_rw_display_widget', 10, 2 );
However this may be time consuming, depending on how many widgets and offers you have and how actively it’s being updated. But you say you posts are categorized with Citytrip category. Is that a category taxonomy or a custom taxonomy? In any case if your posts are assigned to Citytrip category that contains subcategories for e.g. with city names (Paris, Berlin, Cracow, etc.) you can do this better, faster, more automatic.
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 );