WordPress.org

Ready to get started?Download WordPress

Forums

Show different sidebar on taxonomy pages when a default is set (5 posts)

  1. ewergreen
    Member
    Posted 2 years ago #

    My theme holds below code in sidebar.php. I want to now make sure I can show a different sidebar when someone is viewing a custom category.

    //Default sidebar
    $selected_sidebar_replacement = 'Sidebar Widgets';
    
    //If is page or single
    if(is_singular()){
    global $wp_query;
    $post = $wp_query->get_queried_object();
    $selected_sidebar_replacement = get_post_meta($post->ID, 'sbg_selected_sidebar_replacement', true);
    
    //If default selected
    if($selected_sidebar_replacement == '0' || $selected_sidebar_replacement == ''){
    $selected_sidebar_replacement = 'Sidebar Widgets';
                        }
    
    // Reset the global $the_post
    wp_reset_query();
    
                    }

    I've already tried the following, where product-brand is the slug of the custom taxonomy an where Gear is an existing (and working on other pages) sidebar, but it didn't work. Any suggestions would be appreciated.

    if(is_tax( 'product-brand' )){ $selected_sidebar_replacement = 'Gear'; }

    Full "sidebar.php" code on pastebin: http://pastebin.com/CLD9DrUz

  2. stvwlf
    Member
    Posted 2 years ago #

    1) Can you post where in your code you are inserting
    if(is_tax( 'product-brand' )){ $selected_sidebar_replacement = 'Gear'; }

    2) You mentioned "viewing a custom category". You are selecting for a custom taxonomy named 'product-brand' - is 'product-brand' a regular category or the name of a custom taxonomy?

  3. ewergreen
    Member
    Posted 2 years ago #

    Thanks for the help stvwlf. Sorry for mentioning "category", I did indeed mean "taxonomy". Don't know why I mixed them up.

    I placed the code after the check to see whether it's a page or single, and before the check to see if it's "default". Might just be as simple as putting it in the wrong place, but would love to get your advice on where to put it to make this work as I'm unable to do so.

  4. stvwlf
    Member
    Posted 2 years ago #

    Hi

    The problem is that the entire if_singular() code block you posted is only executed when its displaying a single post or a page. If you put the test for is_tax() inside that code block, is_tax() will never be true because a taxonomy archive is not a single post or page so can't be true inside a block executing in a singular page.

    The test for is_tax() must be outside that block.

    I didn't test this code so its possible it might need some tweaking. But it looks right and has the essence of what is needed. ( You don't need to change the query because you aren't looking for a Custom Field which is what the singular code is doing. )

    //Default sidebar
    $selected_sidebar_replacement = 'Sidebar Widgets';
    
    if( is_tax( 'product-brand' )){
       $selected_sidebar_replacement = 'Gear'; 
    
         //If is page or single
    } elseif(is_singular()){
    	global $wp_query;
    	$post = $wp_query->get_queried_object();
    	$selected_sidebar_replacement = get_post_meta($post->ID, 'sbg_selected_sidebar_replacement', true);
    
    	//If default selected
    	if($selected_sidebar_replacement == '0' || $selected_sidebar_replacement == ''){
    		$selected_sidebar_replacement = 'Sidebar Widgets';
    	}
    
    	// Reset the global $the_post
    	wp_reset_query();
    }
  5. ewergreen
    Member
    Posted 2 years ago #

    Thanks stvwlf. For some reason that doesn't work either. I'll try tweaking it a bit later.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags