首页 > 解决方案 > 在 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();

标签: phpwordpress

解决方案


试试这个代码,它使用检查第 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;
?>

推荐阅读