0

Within my loop I want to display 9 posts that exclude categories X, Y, Z and all sticky posts.

Between post 3 and 5 I would like to place a sticky post with a custom template.

The below works except that it treats the sticky post as the 4 post from the first loop which results in one regular post missing.

I have to use WP 3.0.3

<?php if (have_posts()) : ?>
    <?php $count = 0;?>
    <?php
      $sticky_posts = get_option('sticky_posts');
      $arguments = array(
          'posts_per_page' => 9,
          'post__not_in'  => $sticky_posts,
          'category__in'  => array(1,2,3,4,5)
      );
    ?>
    <?php query_posts( $arguments );?>

    <?php while (have_posts()) : the_post(); ?>

        <?php $count++; ?>

        <?php if ($count == 4) : ?>

            <?php
              $sticky = get_option('sticky_posts');
              $args = array(
                  'posts_per_page' => 1,
                  'post__in'  => $sticky
              );
              ?>
              <?php query_posts( $args );?>

              <?php while (have_posts()) : the_post(); ?>

              <h1>Latest Sticky Post</h1>
              <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
              <?php the_excerpt(); ?>

              <?php endwhile; ?>
              <?php wp_reset_postdata(); ?>

        <?php else : ?>

            <h1>Regular Post</h1>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            <?php the_excerpt(); ?>

        <?php endif; ?>
    <?php endwhile;  ?>
<?php endif; ?>
1
  • You should (really) not wrap a loop inside a loop. Better is to save your query results into different vars and loop them affterwards.
    – kaiser
    Commented May 10, 2011 at 16:24

1 Answer 1

1

you don't need if/else on the counter, just check if $count == 3 or 5 to inject a sticky post, insert a regular post in every iteration of the loop, and increment the counter at the bottom.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.