404 on category.php pagination

  • My blog post page : mysite.com/blog => Work
    • Blog page pagination : mysite.com/blog/page/2 =>
      Work
    • A category post url : mysite.com/blog/cat => Work
    • A post url : mysite.com/blog/cat/postname => Work
    • A category post pagination url : mysite.com/blog/cat/page/2 =>
      404 🙁
    • Permalink structure : /blog/%category%/%postname%/
    • Remove category base with Yoast: ok

Category.php :

<?php get_header(); ?>
<?php get_sidebar('page'); ?>
<div class="posts-list">
    <ul class="categs-list">
        <li class="<?php if(is_home()):?>current-cat<?php endif;?>"><a href="https://wordpress.stackexchange.com/questions/256704/<?php echo get_permalink(get_option("page_for_posts')); ?>" title="<?php _e('Voir tous les articles', 'fc'); ?>"><?php _e('Tous', 'fc'); ?></a></li>
        <?php wp_list_categories('title_li='); ?>
    </ul>
    <?php pagination(); ?>
    <?php 
    while ( have_posts() ) : the_post();
        $category = get_the_category();
        get_template_part( 'content-category', $category[0]->slug );
    endwhile;
    ?>
    <?php pagination(); ?>
</div>

<?php get_footer(); ?>

Function pagination() :

function pagination()
{

    if ( function_exists('wp_pagenavi') )
    {
        echo "<nav class="pagination">";
            wp_pagenavi();
        echo "</nav>";
    }

}

Why do i get 404 with pagination ?

Edit: mysite.com/blog/cat/feed doesn’t work either

1 Answer
1

Thanks to this and this i was able to adapt the solution to my problem.

Solution:

add_action( 'init', 'wpa58471_category_base' );
function wpa58471_category_base() {
    add_rewrite_rule(
        'blog/([^/]+)/page/(\d+)/?$',
        'index.php?category_name=$matches[1]&paged=$matches[2]',
        'top' 
    );
    add_rewrite_rule( 
        'blog/([^/]+)/(feed|rdf|rss|rss2|atom)/?$',
        'index.php?category_name=$matches[1]&feed=$matches[2]', 
        'top' 
    );
}

Leave a Comment