2

我有这个自定义功能来显示 WordPress 帖子:

echo evertstrap_conditional_posts( [
    'numberposts' => 3,
    'category' => $term_id,
    'post_class' => 'horizental-post',
    'first_full_width' => true,       
    'thumb_size' => 'latest-thumb',                     
    'boostrap_class' => 'col-6 col-sm-6 col-md-12',
    'show_thumb_caption' => true,
    'read_more' => true,
    'ad_id' => $ad_id,
    'ad_loop_offset' => 2               
] );

这个函数代码是这样的:

<?php
function evertstrap_conditional_posts( $args = null ) {

    global $post;

    $default = array(
        'post_type' => 'post',
        'numberposts' => 10,            
        'bootstrap_class' => 'col-6 col-sm-12 col-md-12',           
        'thumb_size'    => 'thumb',
        'show_excerpt'  => true,
        'show_author'   => true,
        'first_full_width' => false,            
    );

    $args = wp_parse_args( $args, $default );
    $recent_posts = get_posts( $args );

    if( $recent_posts ) {

        $output = '';
            $read_more = isset( $args['read_more'] ) ? $args['read_more'] : '';
            $term_id = isset( $args['category'] ) ? $args['category'] : '';
            $class = isset( $args['bootstrap_class'] ) ? $args['bootstrap_class'] : '';
            $first_full_width = isset( $args['first_full_width'] ) ? $args['first_full_width'] : '';

            // For advertisement
            $ad_id = isset( $args['ad_id'] ) ? $args['ad_id'] : '';
            $ad_loop_offset = isset( $args['ad_loop_offset'] ) ? $args['ad_loop_offset'] : ''; // How many loop the ad should offset


            $counter = 1;
            foreach ( $recent_posts as $post ) {
                setup_postdata( $post );
                $post_id = get_the_id();

                //  some more code here......

                $output .= "<div class='{$conditional_bootstrap_class}'>";
                    $output .= '<article class="'.$conditional_article_class.'">';
                        $output .= '<div class="post-thumb">';                               
                        $output .= '</div>';

                        $output .= '<div class="post-meta">';                              
                        $output .= '</div>';                      
                    $output .= '</article>';
                $output .= "</div>";                    


                // Show advertisement on every 5 posts                  
                if( $counter % $ad_loop_offset == 0  ) {                        
                    $output .= '<div class="col-md-12">';
                        $output .= dynamic_sidebar( $ad_id );
                    $output .= '</div>';
                }

                $counter++;
                // Show advertisement on every 5 posts end here
            } // Foreach end here

        //$output .= '</div>'; // article wrapper class 
        wp_reset_postdata();
        return $output;         
    }
}

现在如果你看到这段代码:

if( $counter % $ad_loop_offset == 0  ) {                        
    $output .= '<div class="col-md-12">';
        $output .= dynamic_sidebar( $ad_id );
    $output .= '</div>';
}

在这里,我想在每 2 个循环上显示 WordPress 动态侧边栏。这$ad_id包含侧边栏的 id。

但它只返回 1 而不是循环之间的内容:(

实际内容显示在整个循环上方:(

你能告诉我为什么吗?我该如何解决?

4

1 回答 1

1

dynamic_sidebar函数在此处记录,我对它的阅读是它将侧边栏作为副作用输出,返回truefalse取决于是否可以找到和调用侧边栏。

基于此,您可以使用输出缓冲来避免过多地更改代码流,如下所示:

if( $counter % $ad_loop_offset == 0  ) {                        
    $output .= '<div class="col-md-12">';

    // start output buffering to capture the output of `dynamic_sidebar`
    ob_start();

    // output the sidebar
    dynamic_sidebar( $ad_id );

    // get the contents of the output buffer
    $output .= ob_get_contents();

    // clean out the output buffer and turn off output buffering
    ob_end_clean();

    $output .= '</div>';
}
于 2020-03-29T11:10:53.310 回答