I would like to retrieve each widget existing in a sidebar in order e.g. to mix them with posts inside the main loop.
I know I can use different widget areas (sidebars) but this approach clutters the widget admin page with some tens of sidebars. To mitigate this, I thought to just add them to a single sidebar and retrieve them consecutively when needed.
But I'm stuck. I've no idea on how to retrieve a widget separately.
My tentative approach is to use wp_get_sidebars_widgets and the_widget but I'm not able to retrieve the widget class name.
Here is a simplified snippet of my code. In this case I'm trying to add a widget every three posts, but is an oversimplification of the logic (since I do not want just to add them regularly) in order to provide you the idea. I'd like to visualize every widget using the_widget
or any other function. How can I accomplish this? Is it possible?
<?php
$i = 1;
$widgets = wp_get_sidebars_widgets(); // I KNOW THE USE OF THIS IS DISCOURAGED (PRIVATE) BUT CANNOT FIND ALTERNATIVES.
if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
if ($i%3 == 0){
echo "<h1>WIDGET #".($i/3)."</h1>";
the_widget($widgets['homepage-1'][$i/3]); // THIS DOES NOT WORKS SINCE I'M NOT GIVING THE CLASS NAME. HOW TO RETRIEVE IT?
}
get_template_part( 'content', get_post_format() );
$i++;
?>
<?php endwhile; ?>
<?php _s_paging_nav(); ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>