3

why the is_active_sidebar() function return always false?

Function.php code:

if ( function_exists('register_sidebar') ) {   
register_sidebar(array( 
    'name' => 'Footer Column 2',
    'id' => 'footer-column-2', // I also added the ID but doesn't work 
    'before_widget' => '<div id="%1$s" class="omc-footer-widget %2$s">',    
    'after_widget' => '</div>', 
    'before_title' => '<h4>',   
    'after_title' => '</h4>'   
));
}

footer.php code:

<?php if ( is_active_sidebar( 'footer-column-2' ) ) : ?>    

    <div class="omc-footer-widget-column">  

            <?php dynamic_sidebar( 'Footer Column 2' ); ?>

    </div><!--- /second-footer-column -->

<?php endif; ?>

I tried with the name or id but not working. Any idea?

Thanks

EDITED: I have more dynamic sidebars and I use this code to register these:

//register custom sidebars
add_action( 'widgets_init', 'register_theme_sidebars_dynamic' );

// and this function...I think the problem is here:

function register_theme_sidebars_dynamic(){
    global $wpdb;
    //post and pages sidebars
    $widgetized_pages = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = 'omc_page_sidebar'", ''));

    if($widgetized_pages){
        foreach($widgetized_pages as $w_page){
            $widget_id = strtolower(str_replace(' ', '_', $w_page));
            register_sidebar(array(
                'name' => $w_page,
                'id'   => 'jw_widgetsection_'.$widget_id,
                'description'   => '',
                'before_widget' => '',
                'after_widget' => '',
                'before_title' => '',
                'after_title' => ''
            ));
         }// For each
    }//End If


   }


1
  • Have you added Widgets to Footer Column 2 sidebar? is_active_sidebar() returns true if the indicated sidebar has Widgets, and returns false otherwise. Commented Jan 15, 2014 at 16:19

1 Answer 1

1

Try this in your functions.php

function your_widget(){

register_sidebar(array( 
    'name' => 'Footer Column 2',
    'id' => 'footer-column-2', // I also added the ID but doesn't work 
    'before_widget' => '<div id="%1$s" class="omc-footer-widget %2$s">',    
    'after_widget' => '</div>', 
    'before_title' => '<h4>',   
    'after_title' => '</h4>'   
));

}

add_action( 'widgets_init', 'your_widget' );

Call in footer.php with the ID.

<?php if ( is_active_sidebar( 'footer-column-2' ) ) : ?>    

    <div class="omc-footer-widget-column">  

            <?php dynamic_sidebar( 'footer-column-2' ); ?>

    </div><!--- /second-footer-column -->

<?php endif; ?>
6
  • Would be helpful to explain why hooking the function onto widgets_init is a fix. Commented Jan 15, 2014 at 16:21
  • Check what Chip Bennet said first. I think you do not put your widget.
    – ucon89
    Commented Jan 15, 2014 at 16:22
  • I know what the answer is, I hoping you could improve your answer with an additional explanation. Commented Jan 15, 2014 at 16:23
  • @AndrewBartel I could not improve with add exp. What I know it is the best practice, and hook sometimes is used by plugin.
    – ucon89
    Commented Jan 15, 2014 at 16:27
  • It's working but what's the problem in my code? I have edited my post, please look at it. Thanks Commented Jan 15, 2014 at 16:43

Your Answer

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

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