[英]How do I insert a <div> after every 5 posts in a WordPress Loop that infinitely loads?
So I have a WPQuery loop set up that works great and uses and infinite scroll to keep loading posts in batches of 10 posts. 因此,我设置了一个WPQuery循环,该循环很好用,并使用无限滚动功能来保持每10个帖子的批次加载。 What I would like to do is insert a <div>
after every 5th post in the list. 我想做的是在列表中每第5个帖子后插入一个<div>
。 I added a counter code to it, but it doesn't seem to give me the desired output. 我向它添加了一个反代码,但它似乎没有给我想要的输出。 Here is the code: 这是代码:
<?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();
}
I can't see what is wrong with the count? 我看不出计数有什么问题吗? I believe it's in the correct place. 我相信它在正确的位置。 Any help would definitely be appreciated. 任何帮助将不胜感激。
Your $counter++; 您的$ count ++++; is after if($counter % 5 == 0). 在if($ counter%5 == 0)之后。 You should move $counter++; 您应该移动$ counter ++;。 above that if because $counter needs to increase value before dividing. 否则,如果$ counter在除法之前需要增加价值。
Try this code: 试试这个代码:
<?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
$counter++;
if ($counter % 5 == 0){
echo '<div>Ads Test Div to Be inserted after every 5th post</div>';
}
?>
<hr>
<?php
}
$counter = 0;
/* Restore original Post Data */
wp_reset_postdata();
}
echo do_shortcode('[ajax_load_more preloaded="false" offset="13" exclude"="'.implode(",", $excludePosts).'" images_loaded="false" button_label="Load more" transition_container="false" progress_bar="false" posts_per_page="10" pause="true" pause_override="true" css_classes="infinite-scroll"]');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.