php - Custom post type url Rewrite (conflicting with page url)

I have a website that has many different custom type posts. I want to use pages as parent templates for those custom typ

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/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/blog/hi-this-is-a-post

However, pagination is broken. www.mysite/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/blog/each-post

Can someone please give me clue? Thanks.

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/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/blog/hi-this-is-a-post

However, pagination is broken. www.mysite/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/blog/each-post

Can someone please give me clue? Thanks.

Share Improve this question asked Mar 23, 2016 at 1:12 Jason MarshJason Marsh 1112 bronze badges 2
  • 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 Commented Mar 23, 2016 at 4:27
  • @Milo Awesome mate, it worked! – Jason Marsh Commented Mar 23, 2016 at 4:52
Add a comment  | 

1 Answer 1

Reset to default 1

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745122249a4612482.html

相关推荐

  • php - Custom post type url Rewrite (conflicting with page url)

    I have a website that has many different custom type posts. I want to use pages as parent templates for those custom typ

    9小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信