Retrieve each widget separately from a sidebar

I am taking the core of the question to be: “… I’m not able to retrieve the widget class name”

You will need to check the global variable $wp_registered_widgets to fill in the missing information. This proof-of-concept code should give you the idea. The code assumes a sidebar named sidebar-1. You will have to adjust that.

global $wp_registered_widgets;
$widgets = wp_get_sidebars_widgets(); 
var_dump($widgets['sidebar-1']); // dump the data
foreach ($widgets['sidebar-1'] as $widget) {
  var_dump($wp_registered_widgets[$widget]); // dump the data
}

For more guidance, take a look at how dynamic_sidebar works, which is basically what I did to work out the above.

Untested, but this was interesting enough that I mocked up some more complete code:

global $wp_registered_widgets;
$i = 1;
$widgets = wp_get_sidebars_widgets(); 
$widgets = $widgets['homepage-1'];
if ( have_posts() ) { 
  while ( have_posts() ) { 
    the_post(); 
    if ($i%3 == 0){
      echo "<h1>WIDGET #".($i%3)."</h1>";
      $cn = $wp_registered_widgets[$widgets[$i%3]]['callback'][0];
      $cn = get_class($cn);
      the_widget($cn,$widgets[$i%3]);
    }
    get_template_part( 'content', get_post_format() );
    $i++;
  }
  _s_paging_nav(); 
} else {
  get_template_part( 'content', 'none' ); 
}

Leave a Comment