0

I was hoping for some advice on the following.

On my homepage, I want to list all posts from all categories except one (category 11) and insert category 11 posts each third post.

e.g.

POST 1 (Post from category '2')

POST 2 (Post from category '7')

POST 3 (Post from '11')

POST 4 (Post from category '5')

POST 5 (Post from category '7')

POST 6 (Post from '11')

POST 7 (Post from category '7')

POST 8 (Post from category '1')

POST 9 (Post from '11')

It all needs to live within while posts for pagination etc.

Could someone advise on the best way to do this?

4

2 回答 2

1

您需要多个循环来实现这一点。您必须创建另一个实例,WP_Query该实例将从类别 11 中获取帖子。然后,正常循环所有其他帖子,同时计数并检查是否显示类别 11 中的帖子。

$page_query_var = 'page';
$post_cat_10 = new WP_Query('cat=10' . '&paged=' . get_query_var($page_query_var));
$other_posts = new WP_Query('cat=-10' . '&paged=' . get_query_var($page_query_var));
for ($i = 1; $other_posts->have_posts(); $i++)
{
    if ($i % 3 === 0 AND $post_cat_10->have_posts())
    {
        $post_cat_10->the_post();
    }
    else
    {
        $other_posts->the_post();
    }
    the_title();
    the_content();
}
于 2012-04-15T13:15:16.660 回答
0

您只需输出每个 POST 但数组中的数字可以除以 3。向您显示可能更容易:

<?php

//Make your SQL-statement within the $result variable

$i = 0;
while( $row = mysql_fetch_assoc($result)) {
    $i++;
    if( $i % 3 > 0) {
        $output[] = $row['COLUMN_NAME'];
    }
}

echo implode("<br>\n", $output);

?>
于 2012-04-15T13:15:18.557 回答