因此,我设置了一个WPQuery循环,该循环很好用,并使用和无限滚动来按10个帖子的批次保持加载。我想做的是在列表中每第5个帖子后插入一个<div>
。我向它添加了一个反代码,但它似乎没有给我想要的输出。这是代码:
<?php
$featuredPosts = get_field('featured_posts');
$excludePosts = [];
foreach($featuredPosts as $key => $postItem) {
$excludePosts[] = $postItem->ID;
}
$numPosts = 10;
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => $numPosts,
'post__not_in' => $excludePosts
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
$counter = 0;
while ( $the_query->have_posts() ) {
$the_query->the_post();
?>
<article id="post-<?php echo get_the_ID(); ?>" <?php post_class('frontpage-articles'); ?> >
<div class="entry-container">
<div class="post-meta"><span class="entry-date"><?php echo get_the_date("M d, Y", get_the_ID()); ?></span>
<span class="entry-author">by
<a href="<?php echo esc_url(get_author_posts_url(get_the_author_meta('ID'))) ?>"><?php the_author(); ?></a>
</span>
</div>
<header class="entry-header">
<h3 class="entry-title">
<?php echo sanitize_title(the_title( '<a href="' . esc_url(get_permalink($post->ID) ) . '">', '</a>' )); ?></h3>
</header><!-- .entry-header -->
<div class="entry-summary">
<?php /*echo substr(strip_tags(get_the_excerpt()), 0,999); */?>
<?php echo substr(strip_tags(get_the_excerpt()), 0, 120); ?>
</div><!-- .entry-summary -->
</div>
<!-- #thumbnail-->
<?php
$cloudinaryImage = get_post_meta($post->ID, 'cloudinary_image_id');
if ( has_post_thumbnail() ) {
$thumbnailUrl = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ) );
}
?>
<?php
$thumbnail = '';
if(!empty($cloudinaryImage[0])) {
$thumbnail = "https://res.cloudinary.com/upload/".$cloudinaryImage[0];
} else if(!empty($thumbnailUrl)) {
$thumbnail = $thumbnailUrl[0];
} ?>
<a href="<?php echo esc_url(get_permalink($post->ID))?> ">
<div class="entry-thumb" style="background-image: url('<?= $thumbnail; ?>'); background-repeat: no-repeat; background-size: cover;" alt="">
</div>
</a>
<!-- .thumbnail-image -->
</article>
<?php
if ($counter % 5 == 0){
echo '<div>Ads Test Div to Be inserted after every 5th post</div>';
}
$counter++;
?>
<hr>
<?php
}
/* Restore original Post Data */
wp_reset_postdata();
}
我看不出计数有什么问题吗?我相信它在正确的位置。任何帮助将不胜感激。
答案 0 :(得分:1)
您的$ count ++++;在if($ counter%5 == 0)之后。您应该移动$ counter ++;。除此之外,如果因为$ counter需要在除法前增加价值。
尝试以下代码:
Add app