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 |1 Answer
Reset to default 1Thank 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
/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 withhas_archive
and use that instead of a parent page. – Milo Commented Mar 23, 2016 at 4:27