Custom post type archive with pagination?

I have a custom post type called ‘projects’ (pastebin) and I have a page called Projects set up in my dashboard which is set to display the Projects template. (pastebin)

The problem is when I use WP_PageNavi to incorporate pagination and click on page 2, I get an error 404. I read somewhere that this is because custom post types and pages can’t have the same name. And I’ve found that to be true because I tried changing the page name to “Projects 2” and it worked.

But when I change the name of the custom post type, that gets reflected in the url. So if I change the custom post type to ‘projects2’ instead of just ‘projects’, the url now looks like this: mysite.com/projects2/single-post

I have my custom permalinks set to /%category%/%postname%/ by the way.

Likewise, I can’t change the name of the page to ‘Projects 2’ for aesthetic reasons.

So how would I get around this? This has been driving me crazy for many hours. I would really appreciate any helpful advice!

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

As you can see in the rewrite analyzer, /projects/page/2/ sets projects=page&page=/2 instead of pagename=projects&paged=2 as we would like. So you just need to add a rewrite rule for this special case (in the register_projects() function, after you register your custom post type, would be a good place):

add_rewrite_rule( 'projects/page/([0-9]+)/?$', 'index.php?pagename=projects&paged=$matches[1]', 'top' );

Remember to flush your rewrite rules, by visiting the Permalinks page or by some other way.

Method 2

Its a better way, you build your own paginatin:

echo paginate_links(array(
  'current' => 5,
  'total' => 9,
  'prev_text' => '« Prev'
));

or via custom solutions, see this 2 posts:


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x