Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 taxonomy.

//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 below (above //If default selected), where "product-brand" is the slug of the custom taxonomy and 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

share|improve this question
add comment

3 Answers

Why don't you simply create a new sidebar... It will be clearer for you and will lighten your sidebar.php.

Create a file in your theme folder : sidebar-thenameyouwant.php put your sidebar code there like this ex :

    <div id="secondary">
        <?php dynamic_sidebar( 'sidebar-xyz' ); ?>
    </div><!-- #secondary .widget-area -->

in your themes function.php... add :

    register_sidebar( array(
    'name' => 'Custom posts sb',
    'id' => 'sidebar-xyz',
    'description' => 'blablabla',
    'before_widget' => '<aside id="%1$s" class="widget %2$s">',
    'after_widget' => "</aside>",
    'before_title' => '<h3 class="widget-title">',
    'after_title' => '</h3>',
) );

then in your custom taxonomy page you call the right sidebar

<?php get_sidebar('thenameyouwant');    ?>

it will automatically load the code from your sidebar-thenameyouwant.php

Of course, the new sidebar will be in your admin Appearance > Widgets... You can slide widgets into it or just edit the new sidebar file directly. If you want to load widgets you have to have the dynamic_sidebar php call like above...

hope this helps.

share|improve this answer
    
Issue is that the theme I'm using is calling the sidebar in the afterloop part, so if I call another sidebar in the "main" part of the code, it'll add a second sidebar. Unfortunatly unless I strip out all code that's currently there, this won't work. Should've maybe specified that, your answer is much appreciated though. –  WouterB Mar 29 '12 at 7:52
    
Tried the below, but it didn't work either. //Default $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 if($selected_sidebar_replacement == '0' || $selected_sidebar_replacement == ''){ $selected_sidebar_replacement = 'Sidebar Widgets';} // Reset global $the_post wp_reset_query(); } –  WouterB Mar 29 '12 at 8:21
add comment

why not just do that ...

if (get_post_type( $post->ID ) == 'xyz' && is_single() ) {
// your code for this sb 
}
else {
// for the other pages/posts
}
share|improve this answer
    
ah sorry you said custom taxonomy not post type. You could maybe use this if it is adapted to your config codex.wordpress.org/Function_Reference/is_post_type_archive –  ThaWolf Mar 29 '12 at 14:16
1  
Also this should help codex.wordpress.org/Taxonomies#Querying_by_taxonomy –  ThaWolf Mar 29 '12 at 14:18
add comment
up vote 0 down vote accepted

I managed to get something working where I just called the taxonomy (unspecific) and compared it to the current taxonomy I was viewing. This meant making too many iterations though, so I removed it and basically added a single conditional to check against the custom_post_type.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.