Trying to achieve WP Pagination with only Prev and Next buttons

I’m trying to modify my code for pagination in such a way that it only displays the Prev and Next buttons with no pages in the middle. This is proving to be kind of hard because paginate_links function doesn’t have many options in terms of arguments which I can pass to disable the pages from being displayed.

Also using next_posts_link() and previous_posts_link() functions doesn’t really help because that links to mydomain.com/page/2 whereas my default homepage is something like this mydomain.com/?fp_type=hot (to display particular kind of custom posts)

Read More

This is my pagination code

<?php
global $wp_query;
global $wp_rewrite;

if( is_search() ){
    if( (int) get_query_var('page') > 0 ){
        $current = get_query_var('page');
    }else{
        if( (int) get_query_var('paged') > 0 ){
            $current = get_query_var('paged');
        }else{
            $current = 1;
        }
    }
    $wp_query = new WP_Query('paged='. $current . '&s='.get_query_var('s') );

    $pagination = array(
        'base' => @add_query_arg( 'paged' , '%#%' ),
        'format' => '',
        'total' => $wp_query -> max_num_pages,
        'current' => $current,
        'show_all' => false,
        'prev_next'=> true,
        'prev_text'=> __('&laquo; Previous','cosmotheme'),
        'next_text'=> __('Next &raquo;','cosmotheme'),
        'type' => 'array'
    );

    if( $wp_rewrite->using_permalinks() ){
            $pagination['base'] = user_trailingslashit( trailingslashit(  remove_query_arg( 'search', remove_query_arg( 's', get_pagenum_link( 1 ) ) ) ) . 'page/%#%/', 'paged' );
    }

    if( !empty($wp_query->query_vars['s'] ) ){
            $pagination['add_args'] = array( 's' => urlencode( get_query_var( 's' ) ) );
    }

    $pgn = paginate_links( $pagination );

    if( !empty( $pgn ) ){
        echo '<div class="pag">';
        echo '<ul class="b_pag center p_b">';
        if( $current == 1 ){
            $current--;
        }
        foreach($pgn as $k => $link){
            print '<li>' . str_replace("'",'"',$link) . '</li>';

        }
        echo '</ul>';
        echo '</div>';
    }
}else{
    $wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;

    $pagination = array(
            'base' => @add_query_arg('paged','%#%'),
            'format' => '',
            'total' => $wp_query->max_num_pages,
            'current' => $current,
            'show_all' => false,
            'type' => 'array'
            );

    if( $wp_rewrite->using_permalinks() ){
            $pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 'fp_type' , remove_query_arg( 'type' , remove_query_arg( 's', get_pagenum_link( 1 ) ) ) ) ) . 'page/%#%/', 'paged' );
    }

    if( !empty($wp_query->query_vars['s'] ) ){
            $pagination['add_args'] = array( 's' => urlencode( get_query_var( 's' ) ) );
    }

    if( !empty( $wp_query->query_vars['type'] ) ){
            $pagination['add_args'] = array( 'type' => get_query_var( 'type' ) );
    }

    if( !empty( $wp_query->query_vars['fp_type'] ) ){
            $pagination['add_args'] = array( 'fp_type' => get_query_var( 'fp_type' ) );
    }

    $pgn = paginate_links( $pagination );
    if( $current == 1 ){
        $current--;
    }

    if(!empty($pgn)){
        echo '<div class="pag">';
        echo '<ul class="b_pag center p_b">';
        foreach($pgn as $k => $link){
            print '<li>' . str_replace( "'" , '"' , $link ) . '</li>';
        }
        echo '</ul>';
        echo '</div>';
    }
} ?>

Related posts

Leave a Reply

1 comment

  1. Add this code to that page where you want to show the post.

     <?php
    $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array(
       'cat' => '7',
       'post_type' => 'post',
       'posts_per_page' => 10,
       'paged' => $page,
       );
    
    query_posts($args);?>
    

    add below code after the post code:

             <?php while ( have_posts() ) : the_post(); ?>
             <div> 
             <ul>
             <li>
              <?php the_content(); ?>
              </li>
             </ul>
             </div>
            <?php endwhile; // end of the loop. ?>
           <div class="navigation"><p><?php posts_nav_link(); ?></p></div>