How would I edit the following code, so that only 2 of the posts are echoed instead of 3
<?php
global $more; $more = false; # some wordpress wtf logic
$num_of_posts = $wp_query->post_count;
$current = -1;
$cat_id = get_cat_ID(single_cat_title('', false));
if(!empty($cat_id))
{
$query_string.= '&cat='.$cat_id;
}
query_posts($query_string);
if (have_posts()) : while (have_posts()) : the_post(); $current++
?>
<?php if($current == 0) { ?>
You just have to check/count how many posts you alreay handled.
The
WP_Query
class has several methods that push various information to the resulting$wp_query
object:$wp_query->found_posts
shows the total number of posts$wp_query->numberposts
the same, but it’s deprecated (still works)$wp_query->posts_per_page
if you got a paged query, you can see how many per page you want to display$wp_query->current_post
is the currently looped through postSo if you want to abort, simply go and
to stop the rest of the loop.
Another option would be to jump in use the
post_limits
filter. The following example summed up as mini-plugin or mu-plugin.You could as well go further and set it during the
parse_request
(earlier) orpre_get_posts
filter.You’ll just have to tweak this to your needs.