0

I have created a home.php file and in that i am showing my custom post type "projects with the WP-Query". I have added pagination to my custom post type using "paginate_links()" and the list shows up but when you click them error not found is shown. here is the code for the loop i am using:

   $args = array(
     'post_type' => 'projects',
     'posts_per_page' => 3,
     'paged'=> $paged,
     'order_by' => 'date',
                );
      $projects = new WP_Query( $args );

      while( $projects -> have_posts() ): $projects -> the_post();
  ///////// the content of the post types
       endwhile;

and this is the code I am using for the pagination links:

    $big = 999999999; // need an unlikely integer
  echo paginate_links( array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ))),
        'format' => '?paged=%#%',
        'current' => max( 1, get_query_var('paged') ),
        'total' => $projects->max_num_pages
                ) );
0

You Can try this way: add the following before $args

        if( get_query_var( 'paged' ) ):
            $my_page = get_query_var( 'paged' );
        else:
            if( get_query_var( 'page' ) ):
                $my_page = get_query_var( 'page' );
        else:
                $my_page = 1;
        endif;
        $paged = $my_page;

Replace pagination code with this(make sure to add this after endwhile):

$total = $projects->found_posts;
            $page = isset( $_GET['page'] ) ? abs( (int) $_GET['page'] ) : 1;
            $format = 'page/%#%/';
            $current_page = max(1, $paged);
            $big = 999999999;
            echo paginate_links( array(
                'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
                'format' => '?page=%#%',
                'end_size'           => 1,
                'mid_size'           => 2,
                'prev_next'          => True,
                'prev_text'          => 'prev',
                'next_text'          => 'next',
                'type'               => 'plain',
                'add_args'           => False,
                'add_fragment'       => '',
                'before_page_number' => '',
                'after_page_number'  => '',
                'total' => ceil($total / $posts_per_page),
                'current' => $current_page,
            ));

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.