【问题标题】:Wordpress first post in different containerWordpress 在不同容器中的第一篇文章
【发布时间】:2020-08-11 05:57:28
【问题描述】:

我是 Wordpress 的新手,但我遇到了帖子循环的问题。我需要先在不同的容器中发布,然后再在其他容器中发布。 我想做这样的模板:

<div class="container-fluid">
    <div class="row">
          first post
    </div>
</div>
<div class="container">
    <div class="row">
          <div class="col-12">
                posts
          </div>
    </div>
</div>

我做了这样的事情,但效果不佳:

<?php if (have_posts()) : $postCounter = 0 ?>
        <?php while (have_posts()) : $postCounter++ ?>
            <?php foreach (array(the_post()) as $post): ?>
                <?php if ($postCounter == 1): ?>
                    <div class="container-fluid">
                        <div class="row">
                            <h1>Tu będzie pierwszy post</h1>
                        </div>
                    </div>
                <?php endif; ?>
                <div class="container">
                    <div class="row">
                        <div class="col-12">
                            <?php if ($postCounter > 1): ?>
                                <h1>Tu będzie kolejny post</h1>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            <?php endforeach ?>
        <?php endwhile; ?>
    <?php endif; ?>

【问题讨论】:

    标签: php wordpress loops posts


    【解决方案1】:

    您可以将布尔值 $first 设置为 true,然后在第一篇文章中将其更改为 false。那么您需要做的就是根据$first 是否为true 添加不同的HTML。

    
        <?php 
        if ( have_posts() ) : 
    
            $first = true;
    
            while ( have_posts() ) : the_post();
                if ( $first ):
                    $first = false;
                    ?>
                    <div>First Post</div>
                    <?php
                else:
                   ?>
                   <div>Not First Post</div>
                   <?php
                endif;
    
            endwhile; 
        endif; 
        ?>
    

    【讨论】:

      【解决方案2】:

      感谢您的回答,但我会使用引导程序。我想在 container-fluid 中发布第一篇文章,但其余帖子希望在容器中没有,我有问题,因为如果我使用您的代码将有自己的容器和行。

      效果如下图

          if ( have_posts() ) : 
      
              $first = true;
      
              while ( have_posts() ) : the_post();
                  if ( $first ):
                      $first = false;
                      ?>
                      <div class="container-fluid">
                            <div class="row">
                                  first post
                            </div>
                      </div>
                      <?php
                  else:
                     ?>
                     <div class="container">
                            <div class="row">
                                  post 2
                            </div>
                      </div>
                      <div class="container">
                            <div class="row">
                                  post 3
                            </div>
                      </div>
                      <div class="container">
                            <div class="row">
                                  post 4
                            </div>
                      </div>
                     <?php
                  endif;
      
              endwhile; 
          endif; 
          ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多