插入HTML以在WP_QUERY循环中关闭和打开div

时间:2015-12-03 作者:C. Ruben

我试图实现的输出是:

<div class="row">
<div class="small-1 large-4 columns"><a href="some link"><img src="some image"></a></div>
<div class="small-1 large-4 columns"><a href="some link"><img src="some image"></a></div>
<div class="small-1 large-4 columns"><a href="some link"><img src="some image"></a></div>
</div>
<div class="row">
<div class="small-1 large-4 columns"><a href="some link"><img src="some image"></a></div>
<div class="small-1 large-4 columns"><a href="some link"><img src="some image"></a></div>
<div class="small-1 large-4 columns"><a href="some link"><img src="some image"></a></div>
</div>
<div class="row">
<div class="small-1 large-4 columns"><a href="some link"><img src="some image"></a></div>
<div class="small-1 large-4 columns"><a href="some link"><img src="some image"></a></div>
<div class="small-1 large-4 columns"><a href="some link"><img src="some image"></a></div>
</div>
这是我的PHP

<?php $first_query = new WP_Query(\'cat=22&posts_per_page=9\'); ?>

<?php while ($first_query->have_posts()) : $first_query->the_post(); ?>

<?php if($first_query->current_post && !($first_query->current_post % 3) ) : ?>
<div class="row">

<?php endif; ?> 

<?php if($first_query->current_post && !($first_query->current_post % 1) ) : ?>  

    <div class="small-1 large-4 columns"><a href="<?php the_permalink(); ?>">    <?php the_post_thumbnail(\'large\'); ?></a></div>

 <?php endif; ?>   

  <?php if($first_query->current_post && !($first_query->current_post % 3) ) : ?>

</div>

<?php endif; ?> 

<?php endwhile; // End the loop.  ?>
所以基本上我需要一个row类的DIV,用small-1-large-4列类封装三个DIV。我在这篇文章中看到的代码是

2 个回复
最合适的回答,由SO网友:birgire 整理而成

您可以通过使用array_chunk() PHP函数,将数组拆分为较小的块。那么你就不必担心用一些数学技巧来打开和关闭div了。

让我们重写您的代码片段,并希望它更易于使用:

// Let\'s get all posts with thumbnail set in some category
$args = [ 
    \'cat\'            => 22,
    \'posts_per_page\' => 9,
    \'meta_key\'       => \'_thumbnail_id\'
];

// Fetch posts
$query = new WP_Query( $args );

if( $query->have_posts() )
{
    while ( $query->have_posts() )
    {
        $query->the_post(); 

        // Collect all items into a temp array    
        $tmp[] = sprintf( 
            \'<div class="small-1 large-4 columns"><a href="%s">%s</a></div>\',
            get_permalink(),
            get_the_post_thumbnail( get_the_ID(), \'large\' )
        );
    } 

    // Split the divs into rows of 3 items
    $rows = array_chunk( $tmp, 3 );

    // Housecleaning
    unset( $tmp );
    wp_reset_postdata();

    // Output the rows
    foreach( $rows as $row )
        printf( \'<div class="row">%s</div>\', join( \'\', $row ) ); 

}
瞧,妈,这里没有数学

希望您可以根据自己的需要进行调整。

SO网友:Milo

数学版;)-

打开并关闭循环外部的第一行/最后一行,然后只需担心每三篇不是最后一篇文章的文章就关闭/打开一行。

echo \'<div class="row">\';

while ( $query->have_posts() ){

    // output content for this post here

    if( 0 == ( $first_query->current_post + 1 ) % 3
        && ( $first_query->current_post + 1 ) != $first_query->post_count ){
            echo \'</div><div class="row">\';
    }

}

echo \'</div>\';