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.

  • Are you using 'paginate_links' for paginataion? Can you please explain a little more. – Sabita Sahoo Oct 31 '14 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 Oct 31 '14 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. – Thien Nguyen Oct 31 '14 at 9:05
0

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 */
    }

}
|improve this answer|||||
  • Thank you Sabita, i will follow your code and tell you the result as soon as possible. – Thien Nguyen Oct 31 '14 at 9:01
  • @ThienNguyen, Is this working ? – Sabita Sahoo Nov 5 '14 at 11:12

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.