I’m using a custom post type in WordPress (called “ns_news_article” generated from Magic Fields) which are registered and working. This rewrites the URL to add “category/news” after the domain (so: www.mydomain.com/category/news/custom-post-1). I have to have the “fake directories” in there.
I have this in the functions.php:
add_rewrite_rule("[ns_news_article]" . '$', "index.php?pagename=[ns_news_article]", "top");
add_rewrite_rule("[ns_news_article]" . '/page/([0-9])*/?', "index.php?pagename=[ns_news_article]" . '&paged=$matches[1]', "top");
global $wp_rewrite;
$wp_rewrite->flush_rules();
The 2nd line takes care of pagination 404 problem when viewing as multiple pages of lists, but then gives 404 errors when trying to view the single post. If I get rid of this, you can view the posts, but pagination doesn’t work.
Can I fix this or should it be done another way?
I’ve recently done custom post types pagination, so this is how I’ve done. Let’s say your custom post type it’s called “customp”.
Create a file in your theme called
page-customp.php
. Then publish an empty page with “Customp” title. Now, when you’re visiting http://www.yourdomain.com/customp you will see a page that is usingpage-customp.php
as a template. Now we will be using this page to show the custom posts and with a pagination.Place this code in
your page-customp.php
file:In your
functions.php
file place this:I hope it helps 🙂