0

This question already has an answer here:

EDIT

Answer was found here: is_active_sidebar() Always Returns False

By user: s_ha_dum


I'm dynamically creating sidebars and registering them for every top level page:

Dynamically Register Sidebars For Each Top Level Page

The problem is, I want to test if a sidebar doesn't exist for that page, to use default:

if(is_dynamic_sidebar(get_the_title()))
                    dynamic_sidebar(get_the_title());
                else
                    dynamic_sidebar('Default');

I've tried to test

if(is_dynamic_sidebar(get_the_title()) && dynamic_sidebar(get_the_title()) but this just repeats the sidebar content twice for some reason. I also tried:

if(is_dynamic_sidebar(get_the_title()) && is_active_sidebar(get_the_title()) but this just removes all my sidebars or if I change the && to || it will display normally except never uses default. So pretty much on my subpages, it uses no sidebar. Anybody have suggestions on what I can do?

Updated Code thanks to @Ravs

if(!is_active_sidebar(get_the_title()))
    dynamic_sidebar(get_the_title());
else 
    dynamic_sidebar('Default');

marked as duplicate by Howdy_McGee, s_ha_dum, kaiser, tfrommen, gmazzap Oct 15 '13 at 23:00

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

The dynamic sidebar code you are using uses the page title of the topmost page to name the sidebar. get_the_title finds the title of the page you are on, not the topmost page title. You need a function that tells you the title of the topmost page in a page hierarchy.

/**
 * Get the topmost page title.
 *
 * @link http://wordpress.stackexchange.com/questions/107843/dynamic-sidebar-conditional
 */
function wpse_107843_get_top_parent_page_title() {
    global $post;

    $parent_page = get_post( $post->ID );

    // Recurse to top parent.
    while ( $parent_page->post_parent != 0 )
        $parent_page = get_post( $parent_page->post_parent );

    return $parent_page->post_title;
}
  • While you are right, I am generating each sidebar with the same name as the page title, so if the sidebar with that page title doesn't exist then go to default. – Howdy_McGee Jul 29 '13 at 13:35
  • Yes. I think I misunderstood the question. :) – Charles Clarkson Jul 29 '13 at 18:50
1

Since you have specific sidebar on parent pages and default sidebar for subpages, your condition may be ( i am assuming your are in wordpress loop )

  1. check for page, parent page or sub page.
  2. check sidebar widget area exist for page if current page is parent page

dynamic_sidebar return True, if widget sidebar was found and called. False if not found or not called.

    if($post->post_parent == 0 ){ // if page is a parent page
      if(dynamic_sidebar(get_the_title()) && is_active_sidebar(get_the_title()))
         dynamic_sidebar(get_the_title());
      else 
         dynamic_sidebar('Default');
    }
    else{ // if page is a sub page
      dynamic_sidebar('Default');
    }

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