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 using this code from another answer

add_action( 'dynamic_sidebar', 'wpse_96681_hr' );

function wpse_96681_hr( $widget ) {

    static $counter = 0;

    // right sidebar in Twenty Ten. Adjust to your needs.
    if ( 'sidebar' === key( $GLOBALS['wp_registered_sidebars'] ) )
         print '<h1>THIS IS SIDEBAR 1</h1>';

    if ( 0 !== $counter && 0 === $counter % 1 )
        print '<hr><h1>test</h1>';

    $counter += 1;
}

The problem is that it can't detect the sidebar correctly key( $GLOBALS['wp_registered_sidebars'] ) always outputs footer-1 that is the last widget area my theme (genesis framework) registers. But it should actually output sidebar (inside that primary sidebar at least) because that is where I am seeing the test output when I remove the 2 lines.

I could use the before- and after-widget-area hooks to add the action there and remove it later but I would prefer a non framework specific version of this code that works everywhere. Any Ideas? Is this code just not working under genesis (my guess) or outdated? Maybe another solution to detect on what widget area the loop is currently on?

// edit: Strange thing (not really) is that this is executed on admin, I see the code there, no problem I can deal with that later, and actually use it to test it and changed the check to === and now its positive on the primary and secondary sidebar for whatever reason. I not get it.

admin widgets On the frontend however "this is sidebar 1" is not displayed at all. Hate shit like this.

share|improve this question

1 Answer 1

$GLOBALS['wp_registered_sidebars'] is a multidimensional array (an array of arrays) so you have to loop over this array and execute the key() function for each member

Now I think I know what is what you want to get. If you want to get the id of the widget's parent sidebar, the widget area where the widget is assigned, you won't be able to do through the global wp_registered_sidebars (maybe I'm wrong but using that global is not working for me both in frontend and admin area). You will have to use the arguments of the widget() method of the widget class:

add_action( 'widgets_init', 'cybnet_widgets_init' );
function curioso_widgets_init() {
     register_widget('cybnet_widget');
}


class cybnet_widget extends WP_Widget {
 function cybnet_widget() {
        $widget_ops = array( 'classname' => 'cybnet_widget', 'description' => ''  );
        $control_ops = array( 'width' =>  '' => '', 'id_base' => '' );
        $this->WP_Widget( 'cybnet_widget','cybnet_widget', $widget_ops, $control_ops );
}

function widget( $args, $instance ) {
            //Here is the id of the widget's parent sidebar
            $parent_sidebar = $args['id'];

    .....Rest of the widget code....
share|improve this answer
    
Well no the key functions checks what key php is currently on. So it makes absolutely no sense to loop over it in this case. If course it will then be true when sidebar gets looped over but I know already that sidebar is a key of that array so this is not moving me forward. I think you not really getting what the purpose if this key check is. –  David Icke Sep 4 '13 at 0:10
    
Please, see the edited answer and tell me if that is what you are looking for. –  cybmeta Sep 4 '13 at 1:34

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.