我有这个自定义功能来显示 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 而不是循环之间的内容:(
实际内容显示在整个循环上方:(
你能告诉我为什么吗?我该如何解决?