$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Loop through Widgets in sidebar using widget array number?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Loop through Widgets in sidebar using widget array number?

matteradmin8PV0评论

Loop through widgets in sidebar

This function from @birgire is almost perfect for me, but how do I call it using the sidebar array as the second parameter, rather than the widget ID. In his example, I would like to do something like this:

wpse_show_widget( 'lobby-sidebar', lobby-sidebar[0] );

In birgire's original post, he gets a Registered Widget called 'sidebar-1' and a registered widget with the ID 'calendar-2'.

Rather than trying to find the specific ID that WordPress assigns to a widget, I'd like the second parameter to be the array ID. That is far easier for me to find than the widget ID.

I modified birgire's function to use wp_get_sidebars_widgets();, but the codex says that's a private function not meant for theme development. I thought someone here might have a more elegant solution.

Loop through widgets in sidebar

This function from @birgire is almost perfect for me, but how do I call it using the sidebar array as the second parameter, rather than the widget ID. In his example, I would like to do something like this:

wpse_show_widget( 'lobby-sidebar', lobby-sidebar[0] );

In birgire's original post, he gets a Registered Widget called 'sidebar-1' and a registered widget with the ID 'calendar-2'.

Rather than trying to find the specific ID that WordPress assigns to a widget, I'd like the second parameter to be the array ID. That is far easier for me to find than the widget ID.

I modified birgire's function to use wp_get_sidebars_widgets();, but the codex says that's a private function not meant for theme development. I thought someone here might have a more elegant solution.

Share Improve this question edited Nov 29, 2018 at 14:34 red5 asked Nov 28, 2018 at 15:06 red5red5 1011 silver badge2 bronze badges 2
  • And what is lobby-sidebar[0]? Because it isn't correct PHP syntax, so it's a little bit hard to guess, what exactly are you trying to achieve... – Krzysiek Dróżdż Commented Nov 28, 2018 at 15:36
  • @KrzysiekDróżdż: sorry for the confusion. I added more explanation. I'll also add my modifications to the original post. I asked a new question rather than commenting on the original because my reputation score is very low. – red5 Commented Nov 29, 2018 at 14:35
Add a comment  | 

1 Answer 1

Reset to default 0

I'm using this modification of the original post by birgire, but I don't know if it's "ok" to use wp_get_sidebars_widgets(); in this manner.

function wpse_show_widget_alt( $index, $id ) {
global $wp_registered_widgets, $wp_registered_sidebars;
    $did_one = FALSE;

    //This built-in func isn't meant for theme or plugin development. 
    //I don't know any other way to list widgets in an array sorted by sidebar.
$theWidgets = wp_get_sidebars_widgets();
$widgetNumber = $theWidgets[$index][$id];

// Check if $id is a registered widget
if( ! isset( $wp_registered_widgets[$widgetNumber] ) 
    || ! isset( $wp_registered_widgets[$widgetNumber]['params'][0] ) ) 
{
    return FALSE;
}

// Check if $index is a registered sidebar
$sidebars_widgets = wp_get_sidebars_widgets();
if ( empty( $wp_registered_sidebars[ $index ] ) 
    || empty( $sidebars_widgets[ $index ] ) 
    || ! is_array( $sidebars_widgets[ $index ] ) )
{
    return FALSE;
}

// Construct $params
$sidebar = $wp_registered_sidebars[$index];
$params = array_merge(
                //array( array_merge( $sidebar, array('widget_id' => $id, 'widget_name' => $wp_registered_widgets[$id]['name']) ) ),
                array( array_merge( $sidebar, array('widget_id' => $widgetNumber, 'widget_name' => $wp_registered_widgets[$widgetNumber]['name']) ) ),
                (array) $wp_registered_widgets[$widgetNumber]['params']
          );

// Substitute HTML id and class attributes into before_widget
$classname_ = '';
foreach ( (array) $wp_registered_widgets[$widgetNumber]['classname'] as $cn )
{
    if ( is_string($cn) )
        $classname_ .= '_' . $cn;
    elseif ( is_object($cn) )
        $classname_ .= '_' . get_class($cn);
}
$classname_ = ltrim($classname_, '_');
$params[0]['before_widget'] = sprintf($params[0]['before_widget'], $widgetNumber, $classname_);         
$params = apply_filters( 'dynamic_sidebar_params', $params );

// Run the callback
$callback = $wp_registered_widgets[$widgetNumber]['callback'];            
if ( is_callable( $callback ) )
{
     call_user_func_array( $callback, $params );
     $did_one = TRUE;
}
return $did_one;}
Post a comment

comment list (0)

  1. No comments so far