0

I have set a query_var,called men, which loads a custom template displaying men's clothes. The rewrite rule is as follows:

add_rewrite_rule('collection/men/?$','index.php?post_type=collection&men=yes','top');

and the template redirect code is this:

if(get_query_var('men') && is_post_type_archive(array('collection'))){
    add_filter('template_include',function(){
        return get_template_directory().'/clothes-men.php';
    });
}

The code works, rewrite working as expected ie. localhost/sample-site/collection/men loads the template. The problem comes when I add standard pagination code, clicking 'Next' results in a 404 error. The link generating the 404 error is localhost/sample-site/collection/men/page/2.

I highly suspect that I need to add a rewrite to make pagination work in this custom template loaded by a query_var but I am a bit mixed up in how to go about it.

What rewrite rule (or any other code) will I need to include in order for pagination to work properly?

1

After a bit of digging over the weekend, I found the solution and indeed, it is a rewrite rule with the paged variable added

add_rewrite_rule('collection/men/page/([^/]+)/?$','index.php?post_type=collection&men=yes&paged=$matches[1]','top');

Pagination now works in the custom template loaded by query variable. i.e. localhost/sample-site/collection/men/page/2 loads the next page instead of a 404 error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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