I have different categories showing over main loop. I want to add a specific category post at even positions of main loop. Example current post list with categories: cat1 cat1 cat1 cat1
required: cat1 catA cat1 catA cat1 catA cat1 catA

I am using this to skip present category post, but couldn't add the desired category post into its place.

<?php if($wp_query->current_post%2==1 && in_category('cat1')) continue; ?>

Being amateur with wp my knowledge is limited. How can I do it?

UPDATE: My logic is to use two loops on index.php, one with category and another excluding that category as this:

                <?php for($y=0;$y<5;$y++) { ?>
                <?php $off_num = ($paged-1)*5+$y; ?>
                <?php if(is_home()) query_posts("posts_per_page=1&cat=-1&offset=$off_num"); ?>
                <?php while ( have_posts() ) : the_post(); ?>
                <?php get_template_part( 'content', get_post_format() ); ?>                 
                <?php endwhile; ?>

                <?php if(is_home()) query_posts("posts_per_page=1&cat=-1,-10&offset=$off_num"); ?>
                <?php while ( have_posts() ) : the_post(); ?>
                <?php get_template_part( 'content', get_post_format() ); ?>                 
                <?php endwhile; }?>

Altogether this code shows 10 posts, do I need to add wp_reset_query or any other function like this? My Pagination too got messed up. Earlier their used to be 577 pages and now after this code, there are 2350 pages and those after 577 give 404 error.

How change I remove above problems?


When query iterates it holds the number of current post (zero-based) in current_post property:

echo $wp_query->current_post % 2 ? 'cat1' : 'catA';
| improve this answer | |
  • Are these cat1 and catA slugs? Ok,if I want to exclude like cat1 from even positions how could it be possible? – busyjax Dec 23 '13 at 12:55
  • @busyjax This is example of alternating output for even and odd posts inside the main loop. Beyond this it's pretty much basic PHP. If you need something more elaborate and involving WP APIs please update your question with details and code you have. – Rarst Dec 23 '13 at 12:57
  • I tried this code, it is printing cat1 and catA. It is not working what I intended for. My question is not this. I want to add a specific category posts at even positions by excluding the other categories present at even positions of main loop – busyjax Dec 23 '13 at 13:12
  • @busyjax ah, you want to insert actual posts into the loop... This might be very involved to do cleanly with pagination and everything. Same thing applies - elaborate on code you have and requirements (context, conditions, pagination, etc). – Rarst Dec 23 '13 at 14:25
  • Rarst, I have updated my question with code – busyjax Jan 2 '14 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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