WordPress 博客第一行 2 列第二行和后续行 3 列

Wordpress Blog first row 2 column Second and continuing rows 3 columns

我想使用 Bootstrap 创建一个 Wordpress 存档博客模板。

第一行应该有第一个 post 为 8 列,第二个 post 为 4 列。

第 2 行和后续行应将 post 列为 4 列。

我相信 php 计数器可以启用此模板。有人知道代码应该怎么写吗?

示例模板:

首先获取所有包含 get_posts() to get your posts, which will return an array of WP_Post 个对象的帖子。然后我会像这样循环浏览帖子:

// Get all our posts, and start the counter
$postNumber = 0;
$args = array(
    'posts_per_page' => 8
);
$posts = get_posts($args);
// Loop through each of our posts
foreach ($posts as $post) {
    // If we're at the first post, or just before a post number that is divisible by three then we start a new row
    if (($postNumber == 0) || (($postNumber+1) % 3 == 0)) {
        echo '<div class="row">';
    }
    // Choose the post class based on what number post we are 
    $postClass = '';
    if ($postNumber == 0) {
        $postClass .= "col-md-8";
    } else {
        $postClass .= "col-md-4";
    }
    echo '<div class="'.$postClass.'">';
    // Print the post data...
    echo $postNumber. " " . $postClass;
    echo "</div>";
    // If we are at the second post, or we're just after a post number divisible by three we are at the end of the row
    if (($postNumber == 1) || (($postNumber-1) % 3 == 0)) {
        echo '</div>'; // close row tag
    }
    $postNumber++; // Increment counter
}

这会给你这样的输出:

你显然需要根据你的模板修改它,但这应该给你一个很好的起点。