Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #6496

    nicod
    Participant

    Hi Bartosz,
    Your restricted widget plugin is great, but I have a question about the plugin. I have a travel website and want to use widgets for showing specific travel offers to specific destinations. For example: I want the widget “travel offers to Berlin” only to be shown in 4 specific posts about Berlin. How can I manage that? And ofcourse I would like the widget ‘travel offers to Paris” only to be shown in posts about Paris, and London-offers only next to London-posts, etcetera. These cities (and more) are in the category ‘Citytrips’ so it is not possible by assigning all those widgets to that category.

    Is that also possible to realize with this function-code:

    function custom_rw_display_widget ($display, $instance) {
    if (is_single(1) && $instance[‘title’] == ‘Your widget title’) { // change Your widget title
    $display = true; // or false if you want to hide it
    }
    return $display;
    }
    add_filter(‘rw_display_widget’, ‘custom_rw_display_widget’, 10, 2);

    with kind regards

    #6497

    Bartosz
    Keymaster

    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 );
    #7436

    Cathyj
    Participant

    Hi there,
    Is there an easier way to do this? Without the code you have above? I’ve tried doing it just by category, but it’s not working. I want to be able to customize the widget for various blog posts.

    Thanks!

    #7438

    Bartosz
    Keymaster

    It’s possible for pages only. Other single posts have to be handled via the code above. Maybe we’ll provide dome interface for this, but it’s not available yet.

    #13844

    sgr_b
    Participant

    Personnally I need it more for posts than for pages and I was surprised and disappointed.
    And for me the use of ID numbers to select posts and pages seems more convenient with very long lists.
    Adding these 2 functionalities would make Restrict Widgets perfect for all uses.

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.