0
votes

I'm trying to limit the number of posts that show on a category page. I was doing this with query_posts, like this

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    if (have_posts()){
        query_posts('posts_per_page=6&paged=' . $paged);

The problem is that the query_posts seems to keep the site from realizing what category page it's on, so it shows posts from all categories. Is there a way to fix this? It seems like there should be a way to force WordPress to "get the category" after the query_posts so that it realizes what category it's on and will then display only the relevant posts?

------ Editing to show code when using WP_query ----

echo '<h4>Latest Resources</h4>'."\n";
    global $post;

    if (have_posts()){
    $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; 
    $the_query = new WP_Query( array(
                 'post_type' => 'post', 
                 'paged' => $paged,
                 'posts_per_page' => 6));   
        while($the_query->have_posts()) { 
        $the_query->the_post(); 
            if($post->post_type == 'page') continue;
            setup_postdata($post);
            $categories = get_the_category();
                echo '<div class="listing">';
1

1 Answers

0
votes

Please do not user query_posts its no good use WP_Query instead like so ie:

EDITED

$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; 
    $the_query = new WP_Query( array( 
    'post_type' => 'page',
    'orderby' => 'date',
    'category_name' => 'slug of your category here',
    'order' => 'DESC',
    'paged' => $paged,
    'posts_per_page' => 2) 
);

more on pagination and WP_Query here.

ADDITIONAL INFO

//usage of the $the_query
while ( $the_query->have_posts() ) : $the_query->the_post();
//pagination
get_next_posts_link(Older', $the_query->max_num_pages);
get_previous_posts_link( Newer', $the_query->max_num_pages);