我正在试图找出在我的index.php页面上的4篇文章之后添加一段文本的方法吗?又是在8个岗位上。
我现在只有一个简单的循环,不知道从哪里开始!谢谢
<?php
get_header();
?>
<main id="primary" class="site-main">
<div class="intro">
<h1 class="intro-text">Hi! I’m an Aussie illustrator living in London.</h1>
</div>
<div class="post-grid grid">
<div class="grid-sizer">
<?php
if ( have_posts() ) :
/* Start the Loop */
while ( have_posts() ) :
the_post();
get_template_part( 'template-parts/content-thumb', get_post_type() );
endwhile;
the_posts_navigation();
else :
get_template_part( 'template-parts/content', 'none' );
endif;
?>
</div>
</div>
</main><!-- #main -->
<?php
get_footer();发布于 2020-11-14 21:15:31
试试这段代码,它使用模数除法操作符来检查第N次迭代。
编辑:我将$counter++;移到while循环的顶部。现在应该能正常工作了。
<?php
if ( have_posts() ) :
/* Start the Loop */
while ( have_posts() ) :
$counter++;
the_post();
get_template_part( 'template-parts/content-thumb', get_post_type() );
// Check for 4th iteration in the loop
if ($counter % 4 == 0) {
echo 'YOUR TEXT HERE';
}
endwhile;
the_posts_navigation();
else :
get_template_part( 'template-parts/content', 'none' );
endif;
?>https://stackoverflow.com/questions/64838572
复制相似问题