0

I have this code on a page called Categories.

<?php
/*
Template Name: Categories
*/

get_header(); ?>

    <section id="primary" class="content-area">

        <main id="main" class="site-main" role="main">

        <header class="page-header">
                <h1 class="page-title">
                    Categories
                </h1>
        </header><!-- .page-header -->

        <?php   

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

        $args = array(
            'post_type' => 'post',
            'posts_per_page' => '4',
            'paged' => get_query_var('paged')
        );
        $the_query = new WP_Query( $args ); ?>

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

            <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

                <?php
                    get_template_part( 'content', 'categories' );
                ?>

            <?php endwhile; ?>

            <?php wp_pagenavi( array( 'query' => $the_query ) ); ?>
            <?php wp_reset_query(); ?>

        <?php endif; ?>

        </main><!-- #main -->
    </section><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

I have set the template in admin to page-categories

The pagination shows up with the correct number of pages to browse, however, when I click on the next page (the link is something like example.com/categories/page/2/ ) I get 404 page.

Why is that? how to fix it?

2 Answers 2

1

The problem was in the same name for category slug "categories" and the same for the custom page that was holding all categories with the same name "categories".

I have solved that by changing categories slug (in permalinks settings) to singular "category" and I have left custom page as plural "categories".

If somebody know about some better solution, please let me know, thanks.

P.S. I was thinking about deleting this question, however, I think thinks is an interesting problem and maybe somebody will find it helpful. But If any moderator think it's ot worth of sharing, you can delete this question.

0

This line:

            'paged' => get_query_var('paged')

Should be set to the $paged var you just set, like so:

            'paged' => $paged

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.