Sara
Sara

Reputation: 39

Call different sidebar in one page template with if statement wordpress

I have 1 single post page template which will display 2 different taxonomy terms, and I like to call 2 different sidebar for these 2 different terms. This post template is "single-recipe.php", in which I call "content-single_recipe.php". In "content-single_recipe.php", I call for 2 different sidebars base on the different terms with a condition statement:

SINGLE-RECIPE.PHP

    while ( have_posts() ) : the_post(); 
        get_template_part( 'content', 'single_recipe' ); 
    endwhile; // end of the loop. 

CONTENT-SINGLE_RECIPE.PHP

    php the_content();       

    // Here are code for sidebars:
        $term = get_the_term_list($post->ID, 'recipe_type'); 

        if ( $term = 'salad' ){
            dynamic_sidebar('sidebar-salad');
        }elseif($term = 'sandwich'){            
            dynamic_sidebar('sidebar-sandwich' );
        }

However, no matter what the $term is, it always call the "sidebar-salad".

Upvotes: 1

Views: 568

Answers (2)

Rene Korss
Rene Korss

Reputation: 5484

get_the_term_list gives you HTML list of terms. Use has_term instead. And comparing is done with ==, not =. Since you use =, which is assigning value to variable, you first if will always be true.

if( has_term( 'salad', 'recipe_type', $post->ID ) ){
    dynamic_sidebar('sidebar-salad');
}
elseif( has_term( 'sandwich', 'recipe_type', $post->ID ) ){
    dynamic_sidebar('sidebar-sandwich');
}

Upvotes: 1

Navnit Mishra
Navnit Mishra

Reputation: 505

 // Sidebars
 if(is_front_page())
    $op_sidebar_id = __('Sidebar Home','options');
// Single page and post type sidebars
elseif(is_attachment())
    $op_sidebar_id = __('Sidebar Attachment','options');
elseif(is_single())
    $op_sidebar_id = __('Sidebar Single','options');
elseif(is_page())
    $op_sidebar_id = __('Sidebar Page','options');
else
    $op_sidebar_id = __('Sidebar Home','options');

After that, I add the normal widgetized section with the variable set:

// If using the No Sidebar template if(is_page_template('no-sidebar.php')) : echo ''; // Else, show the sidebar else : echo "";

    if(function_exists('dynamic_sidebar') &&   
  dynamic_sidebar($op_sidebar_id)) :
    else :
    // Default to Home page sidebar if no widgets are set
        if(function_exists('dynamic_sidebar') && dynamic_sidebar(__('Sidebar
   Home','options'))) :
        else : _e('Add content to your sidebar through the widget control  
   panel.','options');
        endif;
    endif;
    echo '</div>';
  endif;

Upvotes: 0

Related Questions