首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在wordpress中的4篇文章后添加部分

在wordpress中的4篇文章后添加部分
EN

Stack Overflow用户
提问于 2020-11-14 21:00:34
回答 1查看 23关注 0票数 0

我正在试图找出在我的index.php页面上的4篇文章之后添加一段文本的方法吗?又是在8个岗位上。

我现在只有一个简单的循环,不知道从哪里开始!谢谢

代码语言:javascript
复制
<?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();
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-14 21:15:31

试试这段代码,它使用模数除法操作符来检查第N次迭代。

编辑:我将$counter++;移到while循环的顶部。现在应该能正常工作了。

代码语言:javascript
复制
<?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;
?>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64838572

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档