【问题标题】:Wrap 3 PHP loop items in a div将 3 个 PHP 循环项包装在一个 div 中
【发布时间】:2019-06-19 11:08:40
【问题描述】:

我正在研究一个 WordPress 主题,但我相信我的问题与 PHP 有关,这就是我在这里发帖的原因。如果我错了,请纠正我。

我正在尝试将一组每 3 个循环项目包装在一个 div 标记中,但有些地方不对劲。下面是我到目前为止所做的代码,但它总是以损坏的 div 结束。

<?php if ( have_posts() ) : $i = 0; while ( have_posts() )  : the_post(); ?>
        <?php if ( $i % 3 ==  0) : ?>
            <div class="articles-loop clearfix">
        <?php endif; ?>
        <article itemtype="https://schema.org/CreativeWork" <?php post_class(); ?>>
            <header class="entry-header">
                <h2 class="entry-title" itemprop="headline">
                    <a href="<?php the_permalink(); ?>" class="entry-title-link" rel="bookmark"><?php the_title(); ?></a>
                </h2>
            </header>
            <div class="entry-content">
                <?php if ( has_post_thumbnail() ) : ?>
                    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                        <?php the_post_thumbnail('generic-grid-archive-featured'); ?>
                    </a>
                <?php endif; ?>

                <?php if ( has_excerpt() ): ?>
                    <p><?php $excerpt = excerpt(23); echo strip_tags($excerpt); ?></p>
                <?php else : ?>
                    <p><?php $content = content(23); echo strip_tags($content); ?></p>
                <?php endif; ?>
            </div>
            <!--
            <footer class="entry-footer">
                <div class="entry-meta clearfix">
                    <p class="read-more"><a href="<?php /*the_permalink(); */?>">Continue Reading</a></p>
                    <p class="author">Published by: <?php /*$author = get_the_author(); echo $author; */?></p>
                </div>
            </footer>
            -->
        </article>
        <?php if ( $i % 3 != 0 ) : ?>
            </div>
        <?php endif; ?>
        <?php $i++; endwhile; ?>
        <?php if ( $i % 3 != 0 ) : ?>
            </div>
        <?php endif; ?>
    <?php else: ?>
        <p><?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?></p>
    <?php endif; ?>

我想要实现的示例:

<div class="articles-loop clearfix">
Loop item 1
Loop item 2
Loop item 3
</div>
<div class="articles-loop clearfix">
Loop item 4
Loop item 5
Loop item 6
</div>
etc.

【问题讨论】:

    标签: php wordpress foreach


    【解决方案1】:

    试试下面的代码:

    <?php
         $loop_counter = $innerBreak = 1;
            $wpb_all_query = new WP_Query(array('post_type'=>'page', 'post_status'=>'publish', 'posts_per_page'=>6));
            if($wpb_all_query->have_posts()):
            while($wpb_all_query->have_posts()) :
            $wpb_all_query->the_post();
            if($innerBreak == 1){
            ?>
    
            <!-- when complete listing of 3 post open the new div -->
            <div class="articles-loop clearfix">
    
            <?php
            }
            ?>
             <article itemtype="https://schema.org/CreativeWork" <?php post_class(); ?>>
                        <header class="entry-header">
                            <h2 class="entry-title" itemprop="headline">
                                <a href="<?php the_permalink(); ?>" class="entry-title-link" rel="bookmark"><?php the_title(); ?></a>
                            </h2>
                        </header>
                        <div class="entry-content">
                            <?php if ( has_post_thumbnail() ) : ?>
                                <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                                    <?php the_post_thumbnail(); ?>
                                </a>
                            <?php endif; ?>
    
                            <?php /*if ( has_excerpt() ): ?>
                                <p><?php $excerpt = excerpt(23); echo strip_tags($excerpt); ?></p>
                            <?php else : ?>
                                <p><?php $content = content(23); echo strip_tags($content); ?></p>
                            <?php endif;*/ ?>
                        </div>
                        <!--
                        <footer class="entry-footer">
                            <div class="entry-meta clearfix">
                                <p class="read-more"><a href="<?php /*the_permalink(); */?>">Continue Reading</a></p>
                                <p class="author">Published by: <?php /*$author = get_the_author(); echo $author; */?></p>
                            </div>
                        </footer>
                        -->
                    </article>
            <?php 
    
            //when complete listing of 3 post closed previously div.
            if($loop_counter%3==0){ echo '</div>'; $innerBreak = 1;}else{$innerBreak = 0;}
    
            $loop_counter++; endwhile; 
            else: 
            echo "<div>No Results Found</div>";
            endif;  
            ?>
    

    【讨论】:

      猜你喜欢
      • 2022-01-23
      • 2011-03-22
      • 2012-10-27
      • 2014-01-22
      • 1970-01-01
      • 2016-11-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多