1

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") );
    }
?>
4
  • Where does is_subpage() come from and what exactly it does? I don't think it is native WordPress function.
    – Rarst
    Sep 30, 2010 at 12:33
  • Yeah, it is not native. I added the function based on suggested code from the codex: codex.wordpress.org/Conditional_Tags Sep 30, 2010 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 }; }; Sep 30, 2010 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. Oct 1, 2010 at 1:13

3 Answers 3

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

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.

0
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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