【问题标题】:wordpress display second three postswordpress 显示第二个三个帖子
【发布时间】:2015-06-08 17:38:42
【问题描述】:

我需要展示:

wordpress 的第一个 div 中的前三个帖子,

第二个div中的第二个三个帖子来自wordpress和

wordpress 的第三个 div 中的第三个三个帖子。

我该怎么做?

div 1
    --1st post
    --2nd tpost
    --3rd post
div 2
    --4th post
    --5th post
    --6th post
div 3
    --7th post
    --8th post
    --9th post

【问题讨论】:

  • 显示你做了什么?

标签: php wordpress posts


【解决方案1】:

您可以通过多种方法做到这一点。将数据放入一个数组,然后chunk() 它,然后循环这些块。

或者,您可以简单地遍历数据:

<div class="...">
    <?php
    // Set counter
    $i = 1;

    // Loop over your posts
    while (have_posts()) {
        // If you have ouputted three already then close and reopen div
        if ($i == 3) {
            $i = 1;

            echo '</div><div class="...">';
        }

        // Output the post and increment the counter
        the_content();
        $i++;
    }
    ?>
</div>

【讨论】:

  • WHILE 应该是什么??
【解决方案2】:

希望代码对你有用。

<?php
$i=0;
if (have_posts() ) : while ( have_posts() ) : the_post();  

if($i==0)
{
?>
<div><!--open div-->
<?php
}
?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
<
$i++;
if($i==3)
{
$i=0;
?>
</div><!--closing div -->  
<?php
}
endwhile; endif;
                

?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-08
    • 2012-06-29
    • 2016-01-05
    • 1970-01-01
    相关资源
    最近更新 更多