You should not be using query_posts
at all, it is just problematic and is prone to failure
Note: This function isn’t meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination)
I don’t even thing that you need to run a custom query at all here, let alone two. I think what you are trying to do here is to split the actual query in two just for the sake of adding your custom field in between post one and two
I would just revert back to the main query and use it as is. The built-in loop counter $wp_query->current_post
is available anywhere in your template, so you can use this to count your posts
This is untested, but you can most probably do something like this. Just add this after your loop. *Remeber, the loop counter starts at zero, not one
if( 0 == $wp_query->current_post ) {
// Add whatever you need to add to appear after post one
}
If you need to paginate and need this after each first post on each page, I would think something like this will work
if( 0 == $wp_query->current_post || 0 == $wp_query->current_post%JUST_ADD_YOUR_POSTS_PER_PAGE_VALUE_HERE ) {
// Add whatever you need to add to appear after post one
}
EDIT
The dynamics has change dramatically, so my first approach won’t work. You will need to run two custom loops just on the fact that both are quite different. I would suggest using WP_Query
in stead of query_posts
. You might even use get_posts
as another alternative.
Just a very important note here, whenever you run custom queries, you have to reset them, otherwise they will influence any other queries that comes after, and I think that might have been your problem in the first place.
Try something like this
<?php $query_one = new WP_Query('cat=4&posts_per_page=5'); ?>
<?php if ( $query_one->have_posts() ) : while ( $query_one->have_posts() ) : $query_one->the_post(); ?>
<h1><?php the_title() ;?></h1>
<?php the_post_thumbnail(); ?>
<?php the_excerpt(); ?>
<?php endwhile; else: ?>
<p>Sorry, there are no posts to display</p>
<?php endif; ?>
<?php wp_reset_postdata(); // VERY VERY IMPORTANT?>
<hr>
<?php // PLACE YOUR FIELD HERE ?>
<?php $query_two = new WP_Query('cat=5&posts_per_page=5'); ?>
<?php if ( $query_two->have_posts() ) : while ( $query_two->have_posts() ) : $query_two->the_post(); ?>
<h2><?php the_title() ;?></h2>
<?php the_excerpt(); ?>
<?php endwhile; else: ?>
<p>Sorry, there are no posts to display</p>
<?php endif; ?>
<?php wp_reset_postdata(); // VERY VERY IMPORTANT?>