Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #30408

    David Borrink
    Participant

    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.eu/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.

    #30558

    David Borrink
    Participant

    Hi, I’m following up here. I’m wondering about the following line in particular…
    if ( $instance['title'] == 'Paris offers' ) {
    Here you are talking about ‘Paris offers’ but what is this referring to in the function of things? The other poster didn’t mention ‘Paris offers’ in his description so I’m wondering what you are referring to.

    I think I understand what’s happening in the next example

    // 1 is an ID of Paris
    		if ( has_term( 1, 'category', $post_id ) ) {
    			$display = true;

    You’re tying the subcategories id number to the parent category here so that it will display in the subcategory only.

    So what am I supposed to replace in the section of ‘Paris offers’ in the first example I show. I understand that if I use the subcategory id numbers in several of the if and esleif statements, I can cover each need. The first bit is tripping me up.

    Thanks!

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