Dynamic Sidebars On Multiple Subpages

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.

Read More

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") );
    }
?>

Related posts

Leave a Reply

3 comments

  1. 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);
        }
    ?>
    
  2. 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.