admin管理员组文章数量:1122832
I've been working with Custom Post Types lately and haven't had any major problems. This time I'm trying something new. I want to create a CPT which basically works like Pages, with page hierarchy.
The real thing is in Swedish so I'll use "Books" as an example on how I would like it to work.
So first I create my new post type:
define('BOOKS_HTTP_PATH', WP_PLUGIN_URL . '/' . str_replace(basename(__FILE__), "", plugin_basename(__FILE__)));
function add_custom_post_type_books() {
register_post_type( 'lpm-books', array(
'labels' => array(
'name' => 'Books',
'singular_name' => 'Book',
'add_new' => 'New book',
'add_new_item' => 'Add new book',
'not_found' => 'No books found' ),
'public' => true,
'show_ui' => true,
'show_in_nav_menus' => true,
'menu_icon' => BOOKS_HTTP_PATH . 'images/books-icon.png',
'menu_position' => 20,
'hierarchical' => true,
'rewrite' => array( 'slug' => 'books'),
'capability_type' => 'page',
'supports' => array('title','editor','page-attributes'),
'query_var' => '',
'_builtin' => false,
)
);
}
add_action( 'init', 'add_custom_post_type_books' );
In my case I have a specific name for the CPT, lpm-books (might sound weird in this example but as I said earlier I'm doing this in Swedish but I'd like the post type name in English if possible). I set the rewrite slug to books for my permalinks and I add 'hierarchical' => true
and 'supports' => 'page-attributes'
to make the Attribute meta box available.
In Admin->Books the view would be something like this:
- Book 1
- Chapter 1
- Chapter 2
- Book 2
- Chapter 1
- Chapter 2
- ...
So, I add a regular page called My books (slug: my-books) to have a "start page" which describes my collection of books. The url is /
. On that page I list all my added books and their chapters.
Works perfectly if I click on Book 1 (or Book 2). The url is /
. But if I want to view a chapter (url: ) I get 404'd.
My permalink structure is: /%category%/%postname%/
I really can't figure out why the custom child pages (posts) 404's. Since my regular start page "My Books" slug doesn't clash with either the CPT name or the rewrite slug I guess I'm all out of thoughts atm.
Note: I've also tried to remove 'rewrite' => array( 'slug' => 'books')
but I still get 404 on the "child posts".
How do you guys do when creating hierarchical CPTs? Can I make this work or do I need to rethink the structure?
Thank you!
I've been working with Custom Post Types lately and haven't had any major problems. This time I'm trying something new. I want to create a CPT which basically works like Pages, with page hierarchy.
The real thing is in Swedish so I'll use "Books" as an example on how I would like it to work.
So first I create my new post type:
define('BOOKS_HTTP_PATH', WP_PLUGIN_URL . '/' . str_replace(basename(__FILE__), "", plugin_basename(__FILE__)));
function add_custom_post_type_books() {
register_post_type( 'lpm-books', array(
'labels' => array(
'name' => 'Books',
'singular_name' => 'Book',
'add_new' => 'New book',
'add_new_item' => 'Add new book',
'not_found' => 'No books found' ),
'public' => true,
'show_ui' => true,
'show_in_nav_menus' => true,
'menu_icon' => BOOKS_HTTP_PATH . 'images/books-icon.png',
'menu_position' => 20,
'hierarchical' => true,
'rewrite' => array( 'slug' => 'books'),
'capability_type' => 'page',
'supports' => array('title','editor','page-attributes'),
'query_var' => '',
'_builtin' => false,
)
);
}
add_action( 'init', 'add_custom_post_type_books' );
In my case I have a specific name for the CPT, lpm-books (might sound weird in this example but as I said earlier I'm doing this in Swedish but I'd like the post type name in English if possible). I set the rewrite slug to books for my permalinks and I add 'hierarchical' => true
and 'supports' => 'page-attributes'
to make the Attribute meta box available.
In Admin->Books the view would be something like this:
- Book 1
- Chapter 1
- Chapter 2
- Book 2
- Chapter 1
- Chapter 2
- ...
So, I add a regular page called My books (slug: my-books) to have a "start page" which describes my collection of books. The url is http://www.example.com/my-books/
. On that page I list all my added books and their chapters.
Works perfectly if I click on Book 1 (or Book 2). The url is http://www.example.com/books/book-1/
. But if I want to view a chapter (url: http://www.example.com/books/book-1/chapter-1
) I get 404'd.
My permalink structure is: /%category%/%postname%/
I really can't figure out why the custom child pages (posts) 404's. Since my regular start page "My Books" slug doesn't clash with either the CPT name or the rewrite slug I guess I'm all out of thoughts atm.
Note: I've also tried to remove 'rewrite' => array( 'slug' => 'books')
but I still get 404 on the "child posts".
How do you guys do when creating hierarchical CPTs? Can I make this work or do I need to rethink the structure?
Thank you!
Share Improve this question edited May 9, 2012 at 10:13 lepardman asked May 9, 2012 at 9:10 lepardmanlepardman 3342 silver badges11 bronze badges1 Answer
Reset to default 2Alright, after a couple more hours I removed the 'query_var' => '',
and re-saved/flushed my permalink settings. Query_var defaults to true, and it works. Hope this can help someone else.
本文标签: url rewritingChild pages on hierarchical Custom Post Types 404s
版权声明:本文标题:url rewriting - Child pages on hierarchical Custom Post Types 404s 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736307972a1933497.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论