0

i have a category named "services". when "our services" is clicked control go to category.php file. in that i need to show all the posts in that service category. i can show all of them in a single page. but i need to show them with pagination where there should be one post per page. i use the below portion to give pagination.

<div class="content-inner">
                    <?php global $cat_name;
     $cat_name = get_category(get_query_var('cat'))->name;
     echo '<h2 class="entry-title">'.$cat_name.'</h2>';

     ?>
                    <?php if ( have_posts() ) : 

                        ?>
                    <?php query_posts('category_name='.$cat_name.'&posts_per_page=1&order=ASC'); ?>
        <?php while (have_posts()) : the_post();?>

         <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

             <br><br>  <div class="popthumb"><h3>

                     <li type="square">  <a href="<?php the_permalink(); ?>" title="<?php //the_title(); ?>"><?php //the_post_thumbnail(); ?><?php  the_title(); ?></a></li>

                 </h3>


 </div>

 <div class="popcontent">
 <?php the_excerpt();?> 

    </div><br>

    </article><br><br>
        <?php endwhile; ?>
                <?php endif; ?>
<div class="alignleft"><?php previous_posts_link('&laquo; Previous') ?></div>
<div class="alignright"><?php next_posts_link('Next &raquo;') ?></div>

i have totally 3 posts in the service category.as per the code it shows only one post per page. but when i click next page not found error is occuring. what should i do?

  • 2
    I would avoid using query_posts - better change the main query with pre_get_posts - see documentation – ptriek Nov 19 '13 at 9:53
  • if that code is on category.php, why are you adding a query at all? as @ptriek already suggested, work with 'pre_get_posts' to set the number to 1 post per page, or set this number generally under settings - reading – Michael Nov 19 '13 at 11:01
0

First of all, never use query_posts(), use WP_Query() instead. (Learn More)

With your WP_Query() use paged parameter in the argument array, like:

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
  'posts_per_page' => 1,
  'paged' => $paged
);

$cat_query = new WP_Query( $args );
?>

And for the pagination link, go with:

<?php
$total_pages = $wp_query->max_num_pages;
if ( $total_pages > 1 ) { ?>

    <div id="nav-below" class="navigation">

        <div class="nav-previous">
            <?php next_posts_link(__( '<span class="meta-nav">&laquo;</span> Older Entries', 'your-theme' )) ?>
        </div>
        <div class="nav-next">
            <?php previous_posts_link(__( 'Newer Entries <span class="meta-nav">&raquo;</span>', 'your-theme' )) ?>
         </div>

    </div><!-- #nav-below -->

<?php } //endif ( $total_pages > 1 )

And don't forget to use wp_reset_postdata() to reset the query after the necessary code block.

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.