php - 在 wordpress 中的 4 个帖子后添加部分
问题描述
我想弄清楚是否有办法在我的 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();
解决方案
试试这个代码,它使用检查第 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;
?>
推荐阅读
- jquery - 在数据表中显示下拉列表
- javascript - 使用 PHP 的 for-each 循环访问嵌套对象的 JSON 数组的所有项目
- python - 如何在系统托盘中显示/隐藏 Windows 控制台?
- microsoft-graph-api - 使用 Microsoft Graph API 获取聊天消息 MIME 的任何方式
- three.js - 使用 raycaster 对 mapbox 上的 3d 对象进行不均匀的点击检测
- c# - 应用程序属性设置缓存在哪里?
- c - C - 汇编错误:无法转换为指针类型
- c# - 使用 IronPdf 将带有图表的 HTML 转换为 PDF
- python - ValueError:未知层:尝试在 keras 中加载模型时的名称
- c++ - C++ chrono 库的限制?