Php 如何使用引导在表格中显示WordPress帖子?

Php 如何使用引导在表格中显示WordPress帖子?,php,wordpress,twitter-bootstrap,loops,Php,Wordpress,Twitter Bootstrap,Loops,我想在WordPress中以这种特定的布局显示我最近的博客文章。以下是: 1|2|3 ----- 4|5|6 这是我到目前为止的代码 <div class="container post_container"> <div class="row"> <div class="col-xs-12"> <h1>Recent Posts</h1> <?php

我想在WordPress中以这种特定的布局显示我最近的博客文章。以下是:

1|2|3
-----
4|5|6
这是我到目前为止的代码

<div class="container post_container">
    <div class="row">
        <div class="col-xs-12">
        <h1>Recent Posts</h1>

            <?php 

            $recentPost = new WP_Query('type=post&posts_per_page=6&order=ASC&');


            if(have_posts()) {
                while($recentPost->have_posts()) {
                    $recentPost->the_post(); ?>


        <br>
        <br>

        <div class="posts_table">
            <--Where all recent posts will be displayed-->
        </div>



                    <?php

                }
            }



             ?>

最近的职位



我想用WordPress循环按时间顺序显示最后六篇文章,我不知道如何创建它,非常困惑。我不知道我应该使用HTML表格还是引导网格,或者两者都使用?

您可以使用
WP\u Query
参数作为数组,这样就不那么容易混淆了

由于要按时间顺序获取帖子,请按
date
排序。类
WP\u查询的实例变成:

$recentPost = new WP_Query(
    array(
        'type'           => 'post',
        'posts_per_page' => 6,
        'order'          => 'ASC',
        'orderby'        => 'date' // Order by date!
    )
);
然后,我看到您正在对行和列使用引导

第一步,围绕while()循环创建所有内容:

最后,我们只需关闭最后一个
.row.posts\u表
元素

        ?>

        </div>
        <!-- end of .row.posts_table -->

        <?php
    }


 ?>
?>
此外,请查看,尤其是


我不知道
.posts\u table
是否对网站有任何意义,如果没有=>它可以被删除。

我想你可能正在寻找类似的东西。请注意,@Joachim提供了一个更深入的答案,但下面是对原始代码的修改版本,这可能是一个很好的参考

<div class="container post_container">
    <div class="row">
        <div class="col-xs-12">
        <h1>Recent Posts</h1>

            <?php 

            $recentPost = new WP_Query('type=post&posts_per_page=6&order=ASC&');


            if(have_posts()) {
                // the wrapper div goes between the IF outside the while
                // added a '.row' class here ?>
                <div class="posts_table row">
                    <?php
                    while($recentPost->have_posts()) {
                        $recentPost->the_post(); 
                            // the column class is added below here inside the WHILE
                            // it needs to output on each loop ?>
                            <div class="col-xs-4">

                                <!-- post content comes here -->

                            </div>

                        <?php

                    } // end the WHILE
                // the .posts_table wrapper is closed here outside the WHILE inside the IF ?>
                </div>
            <?php
        } // end the IF




             ?>

最近的职位

您必须修改php以包含您想要的表或网格(无论是什么)的标记。您可能需要修改或创建wp视图文件以最好地完成此操作。那么您是说它不能手工编码?这就是我的目标。它可以被编码到你提供代码的这个主题模板中。我认为评论中存在误解。@HappyCarts的意思是,你需要在这个主题模板中包含html标记——我想你的意思是,你需要知道代码是什么以及放在哪里。Bootstrap在一段时间前进行了调整,如果其中的列总数超过12,那么它就可以正常工作——它只是自己换行了。如果添加6个带有
.col-xs-4
的元素,它将正常工作,并换行成两行大小相等的行。我不知道。这是一个很好的发展。因此,如果您有一个新的引导版本,您可以跳过棘手的部分。是的:)为了简单起见,它通常可以省略。显然,尽可能使用
.row
进行包装是一种很好的做法,但有时额外的努力并不值得。这可能更容易让TS使用。我倾向于长篇大论。@Joachim我有时也有类似的长篇大论问题哈哈。老实说,我正在编写一个更完整的答案类似于你的答案时,你的答案突然出现,所以我只是粘贴了一个非常简单的例子代码的基础上的原始代码OP张贴。我认为这两个例子更有可能增加OP从这个问题中得到的好处。
            <?php

            // Increment for next post
            $counter++;

            // Use the modulo to break a Bootstrap row and start a new one
            // The HTML inside this control flow will be printed every third post (every third iteration).
            if($counter % 3 == 0){
                ?>
                </div>
                <div class="row posts_table">
                <?php
            }

        }
        ?>

        </div>
        <!-- end of .row.posts_table -->

        <?php
    }


 ?>
<div class="container post_container">
    <div class="row">
        <div class="col-xs-12">
        <h1>Recent Posts</h1>

            <?php 

            $recentPost = new WP_Query('type=post&posts_per_page=6&order=ASC&');


            if(have_posts()) {
                // the wrapper div goes between the IF outside the while
                // added a '.row' class here ?>
                <div class="posts_table row">
                    <?php
                    while($recentPost->have_posts()) {
                        $recentPost->the_post(); 
                            // the column class is added below here inside the WHILE
                            // it needs to output on each loop ?>
                            <div class="col-xs-4">

                                <!-- post content comes here -->

                            </div>

                        <?php

                    } // end the WHILE
                // the .posts_table wrapper is closed here outside the WHILE inside the IF ?>
                </div>
            <?php
        } // end the IF




             ?>