0

I am creating a page template in WordPress to show all my posts. But i have a problem with pagination. I have a page url like below:

http://localhost/sdd/video

"video" is my page slug. And the problem is when i click to page 1 (e.g) then i have url: http://localhost/sdd/video/page/1 but the page show error 404 not found this page. Somebody help me? thanks.

3
  • Are you using 'paginate_links' for paginataion? Can you please explain a little more. Commented Oct 31, 2014 at 4:57
  • Flush permalink. You quite likely need to simply visit your permalink settings page to refresh. Goto Admin > Settings > Permalinks. Then try your URL on the front-end again.
    – Robert hue
    Commented Oct 31, 2014 at 4:58
  • @Roberthue here is my permalink setting: http://localhost/sdd/%postname% <br/> @SabitaSahoo i'm using get_pagenum_link to generate the link. Commented Oct 31, 2014 at 9:05

1 Answer 1

1

Below code will fetch all posts from all categories. Here we have given limit for one page as 10. Then you have to check the permalink structure of ur site. Please check the code I think it will work.

     /* it will fetch all posts */
        $post_args = array(
                'posts_per_page'  => -1,
                'orderby'         => 'post_date',
                'order'           => 'DESC',
                'post_type'       => 'post',
                'post_status'     => 'publish',
            );
        $all_posts = get_posts( $post_args );
        $post_count = count( $all_posts );

        /* if post_count is greater than 0 */
        if ( $post_count > 0 ) {
            $limit = 10;

            /* this is for getting the last post number */
            if ( get_query_var( 'paged' ) ) {
                $current_page = get_query_var( 'paged' );
            } else {
                $current_page = 1;
            }

            $permalink_structure = get_option( 'permalink_structure' );
            $format              = empty( $permalink_structure ) ? '&page=%#%' : 'page/%#%/';
            $total_pages         = ceil( $post_count / $limit );
            $start               = $current_page * $limit - $limit;

        }

        /* it will fetch posts under limit for all categories */
        $post_args = array(
            'posts_per_page'  => $limit,
            'offset'          => $start,
            'orderby'         => 'post_date',
            'order'           => 'DESC',
            'post_type'       => 'post',
            'post_status'     => 'publish',
        );

        $current_posts = get_posts( $post_args );
        ?>
        <!-- this is for showing pagination -->
        <div class="tablenav">
            <div class="tablenav-pages">
                <?php
                echo paginate_links(
                    array(
                        'current'   => $current_page,
                        'prev_text' => '&laquo; ' . __( 'Prev' ),
                        'next_text'     => __( 'Next' ) . ' &raquo;',
                        'base'          => add_query_arg( 'paged', '%#%' ),
                        'format'    => $format,
                        'total'     => $total_pages
                    )
                );
                ?>
            </div>
        </div>
        <!-- then here you can run the loop to show posts from $current_posts -->
<?php

/* if the posts are present */
if ( !empty( $current_posts ) ) {

    foreach ( $current_posts as $post ) {
        /* the code for showing posts */
    }

}
2
  • Thank you Sabita, i will follow your code and tell you the result as soon as possible. Commented Oct 31, 2014 at 9:01
  • @ThienNguyen, Is this working ? Commented Nov 5, 2014 at 11:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.