Add next and previous post links on pages like category, tags or archive pages

I am trying to find a solution for getting next pages for category or tag page in WordPress, tried with next_posts_link and previous_posts_link in these pages, but it's not working.

Same functions are working on post listing page for me

Below is my code

div class="wrapper inner_content_wrap"


          ?php next_posts_link( 'span class="label iconfont"img src="'.get_template_directory_uri().'/images/pager_arw.png" alt=""/spanspan class="entry-info-wrap"Next Entries/span',3); ?
          ?php previous_posts_link( 'span class="label iconfont"img src="'.get_template_directory_uri().'/images/pager_arw2.png" alt=""/spanspan class="entry-info-wrap"previous Entries/span'); ?





        div class="blog_area"
            div class="container"

           ?php

             if(have_posts()) :
            $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
             $args = array( 'post_type' = 'post', 'posts_per_page' = 3, 'paged' = $paged );
             $loop = new WP_Query( $args );
             $i = '1';

             while ( $loop-have_posts() ) : $loop-the_post();  ?
                  ?php if($i=='1' || $i%3 =='1') { echo 'div class="blog_post first"';}  else { echo 'div class="blog_post"';} ? 
                        div class="date"?php the_time('M');  ?strong?php the_time('j'); ?/strong/div                    
                        div class="imgbox" ?php  $url = get_the_post_thumbnail($post_id, 'blog_thumb');
               echo $url; ?/div
                        div class="content"
                            h5a href="?php the_permalink(); ?"?php the_title(); ?/a/h5
                            p?php the_excerpt(); ?a href="?php the_permalink(); ?"[...]/a/p

                            div class="bottom_box"
                                ?php $categories = get_the_category(); 
                                foreach($categories as $category) {
                                    echo 'a href="'.get_category_link( $category-term_id ).'" class="button" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category-name ) ) . '"'.$category-cat_name.'/a';
                                    }?



                                div class="clear"!-- --/div
                            /div
                        /div
                    /div
            ?php 
            $i++; 
            endwhile; 
            else:
                get_template_part( 'content', 'none' );

                endif;

            wp_reset_postdata();

 ?


                div class="clear"!-- --/div
            /div
        /div
    /div

Topic next pagination links Wordpress

Category Web


Two problems I can see off hand here

Firstly, you should move your query to outside your if conditional statement.

Secondly, when using WP_Query, the $max_pages parameter should be used when using next_posts_link( $label , $max_pages );

So, your code should look something like this

<div class="wrapper inner_content_wrap">

  <?php next_posts_link( '<span class="label iconfont"><img src="'.get_template_directory_uri().'/images/pager_arw.png" alt=""></span><span class="entry-info-wrap">Next Entries</span>', $loop->max_num_pages); ?>
  <?php previous_posts_link( '<span class="label iconfont"><img src="'.get_template_directory_uri().'/images/pager_arw2.png" alt=""></span><span class="entry-info-wrap">previous Entries</span>'); ?>

        <div class="blog_area">
            <div class="container">


<?php   
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

$args = array( 
    'post_type' => 'post', 
    'posts_per_page' => 3, 
    'paged' => $paged 
);

$loop = new WP_Query( $args );  

if($loop->have_posts()) :

    $i = '1';

    while ( $loop->have_posts() ) : $loop->the_post();  ?>

<-----REST OF YOUR CODE------->

Try using get_next_posts_link(); right after or outside your while loop. So you'd use it something like:

<?php echo get_next_posts_link(); ?>
<?php echo get_previous_posts_link(); ?>

For more information:

http://codex.wordpress.org/Function_Reference/get_next_posts_link

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.