0

I have a website that has many different custom type posts. I want to use pages as parent templates for those custom type posts (listing all particular custom posts)

For example I have a page called "Blog" and it has URL www.mysite.com/blog It will list custom posts called "blog". Following is the custom post type implementation:

$args = array (
            'label' => 'Blog Posts',
            'singular_label' => 'Blog Post',
            'public' => true,
            'publicly_queryable' => true,
            'show_ui' => true,
            'capability_type' => 'post',
            'hierarchical' => false,
            'rewrite' => array('slug' => 'blog'),
            'query_var' => true,
            'menu_icon' => null,
            'menu_position' => 2,
            'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'post-formats', 'custom-fields', 'editors')
    );

    register_post_type( 'blog' , $args );

This works and each I can click and view each blog posts with URL www.mysite.com/blog/hi-this-is-a-post

However, pagination is broken. www.mysite.com/blog/page/2/ returns 404. I found a workaround by appending /post/ to the URL of the custom post like

'rewrite' => array('slug' => 'blog/post'),

This doesn't break pagination nor link to posts but this is not what we want. We want to have URL that's like

www.mysite.com/blog/each-post

Can someone please give me clue? Thanks.

  • pagination for singular pages don't contain /page/, it's just /blog/2/. you could add a rewrite rule to handle it, but a simpler way is to register the post type with has_archive and use that instead of a parent page. – Milo Mar 23 '16 at 4:27
  • @Milo Awesome mate, it worked! – Jason Marsh Mar 23 '16 at 4:52
0

Thank to @Milo I was able to solve this

1) Delete anything that might be reserving /blog/ url. In my case, It was a singular page with URL /blog/.

2) Modify custom post type declaration so it has 'has_archive' => true

3) Create archive-blog.php and move your code here. (You can just use global query object for this custom post type)

4) Refresh permalink few times by settings it back to plain to custom (It actually refreshes your URL permalink.

5) Works.

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.