Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress custom post pagination 404 error

I've been looking around all day for an answer but no luck.

I have made a custom post type so I can display posts on the page template but pagination is not working .I get a 404 error.

Maybe some of you can guide me and tell me what is the problem.Here is the site.

Use this password broncosrule. Thank You.

Here is the code .

Custom page template:

<?php
/**
 * Template Name: Custom Page
 * The custom page template file
 */ 
?>

<?php get_header(); ?>



<?php 

  $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;

  $custom_args = array(
      'post_type' => 'trestleboard',
      'posts_per_page' => 1,
      'paged' => $paged

    );

  $custom_query = new WP_Query( $custom_args ); ?>

  <?php if ( $custom_query->have_posts() ) : ?>

    <!-- the loop -->
    <?php while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
      <article class="loop">
        <!--<h3><?php the_title(); ?></h3>-->
        <div class="content">
          <?php the_content(); ?>
        </div>
      </article>
    <?php endwhile; ?>
    <!-- end of the loop -->

    <!-- pagination here -->
    <?php
      if (function_exists(custom_pagination)) {
        custom_pagination($custom_query->max_num_pages,"",$paged);
      }
    ?>

  <?php wp_reset_postdata(); ?>

  <?php else:  ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
  <?php endif; ?>

<?php get_footer(); ?>

Functions.php file

// Our custom post type function
function custom_pagination($numpages = '', $pagerange = '', $paged='') {

  if (empty($pagerange)) {
    $pagerange = 2;
  }

  /**
   * This first part of our function is a fallback
   * for custom pagination inside a regular loop that
   * uses the global $paged and global $wp_query variables.
   * 
   * It's good because we can now override default pagination
   * in our theme, and use this function in default queries
   * and custom queries.
   */
  global $paged;
  if (empty($paged)) {
    $paged = 1;
  }
  if ($numpages == '') {
    global $wp_query;
    $numpages = $wp_query->max_num_pages;
    if(!$numpages) {
        $numpages = 1;
    }
  }

  /** 
   * We construct the pagination arguments to enter into our paginate_links
   * function. 
   */
  $pagination_args = array(
    'base'            => get_pagenum_link(1) . '%_%',
    'format'          => 'page/%#%',
    'total'           => $numpages,
    'current'         => $paged,
    'show_all'        => False,
    'end_size'        => 1,
    'mid_size'        => $pagerange,
    'prev_next'       => True,
    'prev_text'       => __('&laquo;'),
    'next_text'       => __('&raquo;'),
    'type'            => 'plain',
    'add_args'        => false,
    'add_fragment'    => ''
  );

  $paginate_links = paginate_links($pagination_args);

  if ($paginate_links) {
    echo "<nav class='custom-pagination'>";
      echo "<span class='page-numbers page-num'>Page " . $paged . " of " . $numpages . "</span> ";
      echo $paginate_links;
    echo "</nav>";
  }

}
like image 456
Agnes Pandek Avatar asked Mar 12 '26 23:03

Agnes Pandek


2 Answers

I think the problem is occurring because your page slug is the same as your custom post type slug.

You can quickly check if this is the issue by temporarily amending the page slug.

Options:

  • Amend the rewrite rule for the custom post type, OR
  • Amend the page slug

Good luck!

like image 153
csknk Avatar answered Mar 14 '26 14:03

csknk


This is work for me please change it with your custom post type name "your_custom_post_type"

 function custom_posts_per_page( $query ) {
        //fixing pagination of custom post type
        if ( $query->is_archive('your_custom_post_type') ) {
            set_query_var('posts_per_page', 1);
        }
    }
    add_action( 'pre_get_posts', 'custom_posts_per_page' );
like image 26
Firefog Avatar answered Mar 14 '26 12:03

Firefog



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!