Get WordPress Sidebars Fom The Current Front End

2019-09-16 09:59发布

问题:

Is there a function to retrieve the WordPress sidebar(s) that is displayed in the current front end?

回答1:

I posted a solution that might work, here: Checking If A WordPress Widget Displayed In The Current Front End

dynamic_sidebar doesn't register that it's been called for that and that sidebar. Neither is there a suitable hook to do this yourself. So I'm afraid you would have to tell Wordpress for each template what sidebars are being displayed. One way to do this would be to create a wrapper function like this

function wrap_dynamic_sidebar( $sidebar_id )
{
    global $sidebars_in_this_template;
    $sidebars_in_this_template[] = $sidebar_id;
    return dynamic_sidebar( $sidebar_id );
}

And replace dynamic_sidebar with this everywhere (but I understand that it is very likely this solution will not be feasible for you).

If you want to display a list of all sidebars, you could use $wp_registered_sidebars

global $wp_registered_sidebars;
$sidebar_ids = array_keys( $wp_registered_sidebars );


标签: wordpress