个性化阅读
专注于IT技术分析

具有不同bootstrap列的WordPress循环

我需要创建一个wordpress循环, 其中第一个帖子将是col-md-12, 接下来的4个帖子将是col-md-6

<div class= "col-md-12">
</div>
<div class= "col-md-6">
</div>
<div class= "col-md-6">
</div>
<div class= "col-md-6">
</div>
<div class= "col-md-6">
</div>

然后

<div class= "col-md-12">
</div>
<div class= "col-md-6">
</div>
<div class= "col-md-6">
</div>
<div class= "col-md-6">
</div>
<div class= "col-md-6">
</div>

#1


可能这可以帮助:

<?php 
if ( have_posts() ) {
     $counter = 0;
    while ( have_posts() ) {

    if(($counter%5) == 0 ){?>
        <div class= "col-md-12"> call the loop tags of the curent post to show data that you want </div>
<?php
}
    else{?>

     <div class= "col-md-6"> call the loop tags of the curent post to show data that you want </div>

    <?php }

    $counter++;
    } // end while
} // end if
?>

如果仅将帖子限制为10个:

   <?php 
    $wp =  new WP_Query(array('posts_per_page'=>10));
    if ( $wp->have_posts() ) {
         $counter = 0;
        while ( $wp->have_posts() ) {

        if(($counter%5) == 0 ){?>
            <div class= "col-md-12"> call the loop tags of the curent post to show data that you want </div>
    <?php
    }
        else{?>

         <div class= "col-md-6"> call the loop tags of the curent post to show data that you want </div>

        <?php }

        $counter++;
        } // end while
    } // end if
    ?>

你必须调用$ wp之类的所有标签, 例如:the_title(), the_content(), 但不是必须的


#2


尝试三元运算符:

<?php 
if ( have_posts() ) {
    $counter = 0;
    while ( have_posts() ) {    
        $colClass = ($counter%5) == 0 ) ? "col-md-12" : "col-md-6";
        echo '<div class= "' . $colClass . '">';
        // Post data
        echo '</div>';   
        $counter++;
    } // end while
} // end if
?>
赞(0)
未经允许不得转载:srcmini » 具有不同bootstrap列的WordPress循环

评论 抢沙发

评论前必须登录!