Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将3个PHP循环项包装在一个div中_Php_Wordpress_Foreach - Fatal编程技术网

将3个PHP循环项包装在一个div中

将3个PHP循环项包装在一个div中,php,wordpress,foreach,Php,Wordpress,Foreach,我正在研究一个WordPress主题,但我相信我的问题与PHP有关,这就是我在这里发帖的原因。如果我错了,请纠正我 我试图在div标记中包装一组每3个循环项,但有些地方不正确。下面是我到目前为止所做的工作的代码,但它总是以坏掉的div结束 <?php if ( have_posts() ) : $i = 0; while ( have_posts() ) : the_post(); ?> <?php if ( $i % 3 == 0) : ?>

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

我试图在div标记中包装一组每3个循环项,但有些地方不正确。下面是我到目前为止所做的工作的代码,但它总是以坏掉的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.

循环项目1
循环项目2
循环项目3
循环项目4
循环项目5
循环项目6
等
尝试以下代码:

<?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;  
        ?>