user1315018
user1315018

Reputation: 1

Every third post pull from excluded category. WordPress

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?

Upvotes: 0

Views: 155

Answers (2)

Fad
Fad

Reputation: 9858

You need multiple loops to achieve this. You'll have to create another instance of WP_Query that will fetch the posts from category 11. And then, Loop through all of the other posts normally while counting and checking whether or not to show a post from category 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();
}

Upvotes: 1

hskrijelj
hskrijelj

Reputation: 433

You simply just output every POST but the number in the array, that can be divided by 3. It might be easier to show to you:

<?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);

?>

Upvotes: 0

Related Questions