0

I am trying to add pagination to the archiveof my custom post type. Right now it is structured by year & month and I want to add pagination and I fail miserably.

So its like this:

<!-- Request: cpt/2014/pagina/2 -->
<!-- Matched Rewrite Rule: cpt/([0-9]{4})/pagina/?([0-9]{1,})/?$ -->
<!-- Matched Rewrite Query: post_type=cpt&amp;year=2014&amp;paged=2 -->
<!-- Loaded Template: index.php -->

And it should load archive-cpt.php like this:

<!-- Request: cpt/2014 -->
<!-- Matched Rewrite Rule: cpt/([0-9]{4})/?$ -->
<!-- Matched Rewrite Query: post_type=cpt&amp;year=2014 -->
<!-- Loaded Template: archive-cpt.php -->

For some special posts I have added a marker and it works. (also pagination fails here as well)

<!-- Request: cpt/marker -->
<!-- Matched Rewrite Rule: evenimente/marker/?$ -->
<!-- Matched Rewrite Query: post_type=cpt&amp;marker=yes -->
<!-- Loaded Template: archive-cpt.php -->

I am hooking myself into init and adding rules like so:

add_rewrite_rule( #works
    $cpt_name. '/marker/?$',
    'index.php?post_type=' . $cpt_slug . '&marker=yes',
    'top'
);

add_rewrite_rule( #fails
    $cpt_name . '/marker/page/([0-9]{1,})/?$',
    'index.php?post_type=' . $cpt_slug . '&paged=$matches[1]&marker=yes',
    'top'
);

What am I doing wrong?

1

Take a look at the "Template Hierarchy" Codex page.

Custom Post Type Archive display

Template file used to render the Archive Index page for a Custom Post Type. [emphasis added]

By my reading, that means that the archive-{cpt_slug}.php template is only used for the page that, by default, lists all posts of that post type in reverse chronological order. I think what you're seeing, at least in terms of the template being used, is the correct expected behavior.

If you want to use a different template—either the archive-cpt.php file or a different specifically for your date-based queries—then you should look into the template_include filter that lets you change which template WordPress uses.

Similar versions of this question have been asked already, so check those out for more help:

  • If site.com/cpt loads archive-cpt.php shouldn't site.com/cpt/page/2 load archive-cpt.php? – aifrim Oct 30 '14 at 16:54
  • Sure, but I think your rewrite rule to add the year is messing things up. – mrwweb Oct 30 '14 at 19:11
  • I will investigate further when I get to work. But those rules are paramount for my project. – aifrim Oct 31 '14 at 12:20
1

Because WordPress redirected to index.php when it should have loaded archive.php or archive-cpt.php I made a workaround like so:

add_rewrite_rule(
    $cpt_name . '/page/?([0-9]{1,})?$',
    'index.php?post_type=' . $cpt_slug . '&qpaged=$matches[1]',
    'top'
);

qpaged instead of paged.

0

You could just use wp pagination instead of using all kind of rewrite rules.

Just use the basic rewrite rules for WordPress. You're CPT will use archive-cpt.php as the archive.

put pagination in your template (http://codex.wordpress.org/Pagination)

Job done (i.e https://medischevacature.nl/vacatures/ cpt = "vacatures")

  • By default CPTS dont have a year/month/day archive. That is why those rewrite rules are in place. – aifrim Oct 30 '14 at 18:13
  • Use php/query them? – Astrid Karsten Nov 6 '14 at 18:43
  • The question has been answered. – aifrim Nov 7 '14 at 9:05

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.