2

Can anyone help me, i have custom post type created with CPT UI plugin, i will add pagination like this: pagination like this one This is my code on my custom template page:

<div class="row">
    <?php
    /**
     * @package Momentum
     * Loop custom post type (journal)
     * @see https://codex.wordpress.org/Post_Types
     */

    /**
     * Pagination
     * @see https://stackoverflow.com/questions/41738362/how-to-include-pagination-in-a-wordpress-custom-post-type-query
     */
    $paged = (get_query_var( 'paged' )) ? get_query_var( 'paged' ) : 1;

    $args = array(
     'post_type'      => 'journal',
     'showposts'   => 2,
     'posts_per_page' => 2,
     'order'          => 'DESC',
     'paged'          => $paged,
    );
    $loop = new WP_Query($args);
    if ($loop->have_posts()):
     while ($loop->have_posts()) : $loop->the_post();
    ?>
    <div class="col-lg-6 content">
     <div class="article">
      <div class="image">
       <?php
       /**
        * @package Momentum
        * Check the featured image
        * @see https://codex.wordpress.org/Post_Thumbnails
        */
       if (has_post_thumbnail()) {
        the_post_thumbnail( 'full', array('class' => 'img-responsiv') );
       }else { ?>
        <img src="<?php echo get_template_directory_uri(); ?>/assets/img/journal-1.jpg" alt="journal">
       <?php
       }
       ?>
       
      </div>
      <div class="text-content">
       <div class="head-content">
        <div class="title">
         <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
        </div>
        <div class="excerpt">
         <?php the_excerpt(); ?>
        </div>
       </div>
       <div class="footer">
        <a href="<?php the_permalink(); ?>" class="read-more pull-left">READ ENTRY</a>
        <span class="date pull-right"><?php echo get_the_date(); ?></span>
       </div>
      </div>
     </div>
    </div><!-- /.col-lg-6 -->
     <?php endwhile; ?>

     <?php
     $total_pages = $loop->max_num_pages;
     if ($total_pages > 1) {
      $current_page = max(1, get_query_var( 'paged' ));
      echo paginate_links( array(
       'base' => get_pagenum_link( 1 ) . '%_%',
       'format' => 'page/%#%',
       'current' => $current_page,
       'total' => $total_pages,
       'prev_text' => __('PREVIOUS'),
       'next_text' => __('NEXT'),
      ) );
     }
     ?>

    <?php else: ?>
     <h1>No Journal found</h1>
    <?php endif; ?>
    <?php wp_reset_postdata(); ?>
   </div>

The pagination link work fine /page/2/...

The problem, when clicked to page/2 the page not found.

I have tried all answear and question on stackoverflow, but not work.

Abd Hannan
  • 153
  • 1
  • 2
  • 11

2 Answers2

1

Finally, fixed the issue when changed has_archive from false to true. I use CPT UI plugin

Abd Hannan
  • 153
  • 1
  • 2
  • 11
1

The problem was WordPress is setup to show 10 posts per page by default which clashed with my query (limiting it to 2 posts) to fix the issue I changed the WordPress setting (Settings / Reading in the admin dashboard) to 1.

oubihis
  • 11
  • 1