对于任何对类似事物感兴趣的人来说,这就是一位职业选手的想法,它是1。为我的小部件插入注册一个特定的侧栏,2。以HTML字符串和3的形式从侧栏获取DOM节点。将其放在一起,同时循环使用可用的小部件。
把这个放进你的functions.php:
// Register sidebar for repeated widgets
function register_custom_sidebar() {
    register_sidebar(array(
        \'name\' => "CTAs — Home",
        \'id\' => \'widgets_home\',
        \'description\' => "Widgets will be displayed after every 3rd post",
        \'before_widget\' => \'<li id="%1$s" class="widget %2$s">\',
        \'after_widget\' => \'</li>\',
        \'before_title\' => \'<h2 class="widgettitle">\',
        \'after_title\' => \'</h2>\',
    ));
}
add_action(\'widgets_init\', \'register_custom_sidebar\');
// Return dom node from other document as html string
function return_dom_node_as_html($element) {
    $newdoc = new DOMDocument();
    $newdoc->appendChild($newdoc->importNode($element, TRUE));
    return $newdoc->saveHTML();
}
 然后使用以下内容创建或调整模板(如本例中的页面模板,任何适合您的模板):
<?php 
/* Template Name: My CTA loop page
 *
 */
get_header();
?>
<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">
        <?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, e.g. "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 "<p>" . the_title() . "</p>"; // 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.    ?>
    </main><!-- #main -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
 当我在没有数据的情况下插入RSS小部件时,我遇到了一些小问题,但除此之外,它似乎工作得很好。