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.

I am building a site in WordPress. It has multiple subpages, many of which require different sidebars. So, I have a widgetized theme and I have also created a few sidebar widgets.

I have written a conditional statement to show different sidebars on different pages. However, one widgetized sidebar displays on almost all pages despite the conditional statement.

The sidebar that is appearing on the desired page can be seen here: http://www.africanhealthleadership.org/about/approach/

The subpage that should have a different sidebar is under Knowledge Resources>Research

The code is as follows. I am a total PHP spazz, so I likely did something silly. I have tried single quotes around dynamic_sidebar(2) but that did not work.

Thank you for any help.

<?php 
     if ( is_subpage('approach') ) {
     if (!function_exists ( dynamic_sidebar(1) ) ) ; 
}
    elseif ( is_subpage('research')) {
    if (!function_exists( dynamic_sidebar(2)) || !dynamic_sidebar( "Sidebar2") );
    }
?>
share|improve this question
    
Where does is_subpage() come from and what exactly it does? I don't think it is native WordPress function. –  Rarst Sep 30 '10 at 12:33
    
Yeah, it is not native. I added the function based on suggested code from the codex: codex.wordpress.org/Conditional_Tags –  Jeff Tancil Sep 30 '10 at 14:11
    
function is_subpage() { global $post; // load details about this page if ( is_page() && $post->post_parent ) { // test to see if the page has a parent $parentID = $post->post_parent; // the ID of the parent is this return $parentID; // return the ID } else { // there is no parent so... return false; // ...the answer to the question is false }; }; –  Jeff Tancil Sep 30 '10 at 14:18
1  
Hi Jeff, if you know specifically what pages you want the sidebar to display on why not just use is_page() and use the page id or slug. Unless I am misunderstanding what your trying to achieve. –  Ash G Oct 1 '10 at 1:13

3 Answers 3

up vote 1 down vote accepted

More easy & elegant (higher maintainability):

<?php 
// Your sidebar should have the wp_meta action hook
wp_meta();

// in ex. your functions.php
function my_sidebar_content() {
    // "About" Page
    if ( is_page('about') ) {
        // If some widget is added via Admin > Design > Widgets
        if ( is_active_sidebar( 'widgets-sidebar-default' ) ) {
            // Display Widgets
            dynamic_sidebar( 'widgets-sidebar-default' );
        }
        // Default Content before Widgets were added
        else {
            _e('default static content', TEXTDOMAIN);
        }
    }
    // "Links" Page
    elseif ( is_page('links') ) {
        if ( is_active_sidebar( 'widgets-sidebar-links' ) ) {
            dynamic_sidebar( 'widgets-sidebar-links' );
        }
        else {
            _e('default static content', TEXTDOMAIN);
        }
    }
}
add_action( 'wp_meta', 'my_sidebar_content', 10 );

# ===================================================
// OR:
wp_meta();

// functions.php
function load_my_sidebars() {
    // "About" Page
    if ( is_page('about') ) {
        get_template_part( 'sidebar_content', 'default' );
    }
    // "Links" Page
    elseif ( is_page('links') ) {
        get_template_part( 'sidebar_content', 'links' );
    }
}
add_action( 'wp_meta', 'load_my_sidebars', 10 );

// in sidebar_content-default.php
    // If some widget is added via Admin > Design > Widgets
    // You can add any static content right here before the widgets
    if ( is_active_sidebar( 'widgets-sidebar-default' ) ) {
        // Display Widgets
        dynamic_sidebar( 'widgets-sidebar-default' );
    }
    // Default Content before Widgets were added
    else {
        _e('default static content', TEXTDOMAIN);
    }
    // You can add any static content right here after the widgets

// in sidebar_content-links.php
    if ( is_active_sidebar( 'widgets-sidebar-links' ) ) {
        dynamic_sidebar( 'widgets-sidebar-links' );
    }
    else {
        _e('default static content', TEXTDOMAIN);
    }
?>
share|improve this answer

I don't know why anybody checks whether dynamic_sidebar exists. It's been around for 9 major releases now. I really hope you're not developing for 2.1 or lower. Try this:

if( is_page('approach') )
  dynamic_sidebar(1);
elseif( is_page('research') )
  dynamic_sidebar(2);

If you know the specific page you want to use, don't bother with that subpage junk. Besides, that function only checks if you're on a subpage in general, but it won't tell you if you're on a specific subpage.

share|improve this answer
    
Thanks, that worked like a charm! –  Jeff Tancil Oct 6 '10 at 1:36

You could just use "Widget Logic" extension so you don't need write support for custom widgets in sidebar into the actual template.

share|improve this answer

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.