WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] query_posts pagination not showing all the pages that it should (3 posts)

  1. josepaitamala
    Member
    Posted 8 months ago #

    Hello,
    I am having quite a weird problem and haven't found how to fix it. Here on the website I am doing the pagination work but only giving me 5 pagination pages,
    http://www.cthawards.com/cth-news/page/5/
    I have more posts that should create at least 2 more pagination pages. If I change the posts_per_page value to 10 instead of 3 then the posts display, but I only want to show 3 posts per page. But when I click the previous link to go to http://www.cthawards.com/cth-news/page/6 , I get that the page doesn't exists.

    Here the code I have, If someone can help me please that would be great! thank you in advance:

    <?php
    // The Query to show only post inside CTH News category
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array(
    'posts_per_page' => 3,
    'cat'      => 1,
    'paged' => $paged
    );
    query_posts($args);
    ?>
    
    <?php /* Start the Loop */ ?>
    <?php while ( have_posts() ) : the_post(); ?>
    <?php //get_template_part( 'content', get_post_format() ); ?>
    <div class="cth-news-content">
    <div class="cth-news-thumb">
    <?php the_post_thumbnail(array(99,99));  ?>
    </div>
    <div class="cth-news-content-text">
    <div class="cth-news-title">
    <h2 class="posttitle"><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php _e( 'Permanent Link to', 'cthawards' ); ?> <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
    </div>
    <?php //Shows the date when the post has been posted
    echo '<div class="cth-single-date">';
    $date = sprintf( '<time class="entry-date" datetime="%1$s">%2$s</time>',
    esc_attr( get_the_date( 'c' ) ),
    esc_html( get_the_date() )
    );
    echo 'Posted on '.$date;
    echo '</div>'; ?>
    <div class="cth-news-excerpt">
    <?php the_excerpt(); ?>
    </div>
    </div>
    </div><!--cth-news-content-->			
    
    <?php endwhile; ?>
    
    <?php cthawards_content_nav( 'nav-below' ); ?>
    <?php // Reset Query
    wp_reset_query();
    ?>
    <?php endif; // end have_posts() check ?>
  2. alchymyth
    The Sweeper & Moderator
    Posted 8 months ago #

    general review
    http://wordpress.stackexchange.com/questions/1753/when-should-you-use-wp-query-vs-query-posts-vs-get-posts?rq=1

    possibly better to use the pre_get_posts method instead of query_posts() to change the query for the main index template:
    http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

  3. josepaitamala
    Member
    Posted 8 months ago #

    Thank you alchymyth

    I did use pre_get_posts and it works ok now. So just so everyone knows in case the need it, I put this code inside the index.php

    <?php if ( have_posts()) : ?>
    
    			<?php /* Start the Loop */ ?>
    			<?php while ( have_posts() ) : the_post(); ?>
                    <div class="cth-news-content">
    					<div class="cth-news-thumb">
    					<?php the_post_thumbnail(array(99,99));  ?>
                        </div>
                        <div class="cth-news-content-text">
                        <div class="cth-news-title">
                    	<h2 class="posttitle"><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php _e( 'Permanent Link to', 'cthawards' ); ?> <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
                        </div>
                       			<?php //Shows the date when the post has been posted
    							echo '<div class="cth-single-date">';
    							$date = sprintf( '<time class="entry-date" datetime="%1$s">%2$s</time>',
    							esc_attr( get_the_date( 'c' ) ),
    							esc_html( get_the_date() )
    							);
    							echo 'Posted on '.$date;
    							echo '</div>'; ?>
                    	<div class="cth-news-excerpt">
                   		<?php the_excerpt(); ?>
                        </div>
                        </div>
                    </div><!--cth-news-content-->
    
    			<?php
    			endwhile;
    			?>
    
    <?php cthawards_content_nav( 'nav-below' ); ?>	  
    
    <?php endif; // end have_posts() check ?>

    and this one on the function.php

    <?php
    ///function to exclude all categories except category 1 on the index.php home
    function include_category( $query ) {
        if ( $query->is_home() && $query->is_main_query() ) {
            $query->set( 'cat', '1' );
    		$query->set( 'posts_per_page', 3 );
    		return;
        }
    }
    add_action( 'pre_get_posts', 'include_category' );
    ?>

Reply

You must log in to post.

About this Topic