Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm working on a category page for a custom post type called articles in WP 3.1, and I'm having some trouble with extracting the page number.

The url: /articles/category/background/

Currently, what works for me, from my functions.php:

add_action( 'init', 'wpse7379_init' );
function wpse7379_init() {
    add_rewrite_rule(
        'articles/category/([^/]+)(/page/([0-9]+))/?$',
        'index.php?post_type=articles&category_name=$matches[1]&paged=$matches[2]',
        'top'
    );
}

This internally rewrites the url to: /index.php?post_type=articles&category_name=background

I'm trying to make the pagination work by expanding the rewrite rule, but it's not working for me.

The pagination works as follows: /articles/category/background/page/2/

New rule:

add_rewrite_rule(
    'articles/category/([^/]+)(/page/([0-9]+))/?$',
    'index.php?post_type=articles&category_name=$matches[1]&paged=$matches[2]',
    'top'
);

This should rewrite the url to: /index.php?post_type=articles&category_name=background&paged=2

I'm using a query with args on my archive-articles.php page, but just a regular loop on the categories.php page, where the posts are displayed.

When I try /index.php?post_type=articles&category_name=background&paged=2 it works, but the url that gets rewritten doesn't. Its just displaying the first page, no matter what I enter as pagenumber.

Does anyone know how to fix this? I think I'm looking for a proper regex, I kinda suck at regexes, so I guess thats the problem.

share|improve this question
    
Are you using a custom query or passing args to query_posts? –  Bainternet Feb 12 '11 at 22:51
    
Added some clarification. I'm using a normal loop on the categories.php page. –  TomvB Feb 13 '11 at 9:42
add comment

1 Answer

up vote 4 down vote accepted

I see three issues in your rewrite rule.

  • The page number is the third, not the second capture group. You count each opening (, so the first is the category name, the second is /page/[0-9]+, and the third is only the [0-9]+ you need. So change the paged parameter to $matches[3].
  • The page part can be optional, so you need to add a ? at the end (this is the reason we put it in a group).
  • The categories can be hierarchical, like /fruit/banana/. For this reason, you should not match them with [^/]+ (any character except a slash), but .+? (any character, but non greedy, so that the rest of the regex can still match).

Together, this results in the following rewrite rule:

add_rewrite_rule(
    'articles/category/(.+?)(/page/([0-9]+))?/?$',
    'index.php?post_type=articles&category_name=$matches[1]&paged=$matches[3]',
    'top'
);

If you don't use it already, I recommend you debug your rules with my rewrite analyzer. You can test them live and see what the query values will be.

share|improve this answer
    
That works, awesome! I really can't thank you enough for both the solution and the explanation, simply amazing :) –  TomvB Feb 13 '11 at 18:07
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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