skip to Main Content

I am using Elementor child theme. I have created a custom post type " project ". I have showed 3 project per page. and I have create a pagination. But this pagination does not work if I don’t save " setting > reading > Blog pages show at most > 3 ". it’s showing me " The page can’t be found ". Althow I have 9 projects. But I don’t want to depend on this " setting > reading > Blog pages show at most ". because next time if I need to show 10 post per page, then it will difficult for me. So, now I want to show 3 projects per page. I can not find where I did wrong. Can anyone help me to solve this, please.

My next page permalink is this " .com/project/page/2/ ". but thi is not working. Only If I set the " setting > reading > Blog pages show at most > 3 " or low number then it’s working.

enter image description here

this is my code:

$paged = get_query_var('paged') ? get_query_var('paged') : 1;
                $args = array(
                    'post_type' => 'project',  // Custom post type name
                    'posts_per_page' => 3,         // Retrieve all posts
                    'paged'          => $paged,
                );
                $query = new WP_Query($args);
                if ($query->have_posts()) :

                    while ($query->have_posts()) : $query->the_post();
                        ?>
                        <div class="col-xl-4 col-md-6">
                            <a href="<?php the_permalink(); ?>" class="ad_project-item">
                                <?php the_post_thumbnail('full'); ?>
                                <h4><?php the_title(); ?></h4>
                            </a>
                        </div>
                    <?php
                    endwhile;

                    // Pagination
                    $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, $paged),
                        'total'     => $query->max_num_pages
                    ));*/

                    $pagination = paginate_links(array(
                        'base' => str_replace($big, '%#%', get_pagenum_link($big)),
                        'format' => 'paged/%#%',
                        'current' => max(1, get_query_var('paged')),
                        'total' => $query->max_num_pages,
                        'prev_text' => __('&laquo; Previous'),
                        'next_text' => __('Next &raquo;'),
                        'type' => 'list',
                    ));
                    var_dump('Max num page :' . $query->max_num_pages);
                    if ($pagination) {
                        echo '<div class="pagination">' . $pagination . '</div>';
                    }

                    // Restore original post data
                    wp_reset_postdata();

                else :
                    echo '<p>No projects found.</p>';
                endif;
                ?>

2

Answers


  1. Chosen as BEST ANSWER

    Thanks for your help. It means I have to change the post type name, I mean it should be " projects ", " client-projects " etc? Am I right?


  2. I experienced the same issue and situation.

    Try the points given below to fix the issue:

    1. The page slug should be different than the post-type project. Check the screenshot below.
    2. Refresh the permalink. Go to the Settings -> Permalinks and Click the save button.
    3. Check the pagination.

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search