< ?php while (have_posts()) : the_post(); ?> < ?php // Your custom query $args = [ "post_type" => "post", "posts_per_page" => "-1" ]; $custom_posts = new WP_Query($args); // Catch output of sidebar in string ob_start(); dynamic_sidebar("widgets_home"); $sidebar_output = ob_get_clean(); // Create DOMDocument with string (and set encoding to utf-8) $dom = new DOMDocument; $dom->loadHTML('< ?xml encoding="utf-8" ?>' . $sidebar_output); // Get IDs of the elements in your sidebar, eg "text-2" global $_wp_sidebars_widgets; $sidebar_element_ids = $_wp_sidebars_widgets["widgets_home"]; // Use ID of your sidebar // Save single widgets as html string in array $sidebar_elements = []; foreach ($sidebar_element_ids as $sidebar_element_id): // Get widget by ID $element = $dom->getElementById($sidebar_element_id); // Convert it to string (function return_dom_node_as_html() must be in functions.php) $sidebar_elements[] = return_dom_node_as_html($element); endforeach; $widget_intervall = 3; // After how many post a widget appears $post_count = 0; $element_count = 0; while ($custom_posts->have_posts()): $custom_posts->the_post(); echo "

" . the_title() . "

"; // Whatever you want to display from your news posts (= main loop) $post_count++; if (!empty($sidebar_elements) && $post_count % $widget_intervall === 0): // Echo the widget echo $sidebar_elements[$element_count]; $element_count++; // Restart after the last widget if ($element_count == count($sidebar_elements)): $element_count = 0; endif; endif; endwhile; wp_reset_postdata(); ?> < ?php // If comments are open or we have at least one comment, load up the comment template if (comments_open() || get_comments_number()) : comments_template(); endif; ?> < ?php endwhile; // end of the loop. ?>