admin管理员组文章数量:1394164
i use this code for a custom post type calles portfolio. Now i want to use a parent-child realtion between the items. For example i have a portfolio item with this URL: www.domain.de/portfolio-item1
Another Portfolio Item should be a child from the portfolio item1 and the url should be
www.domain.de/portfolio-item1/portfolio-item2
Im a beginner with php and i dont know how i can realise this. Can somebody help me?
// Portfolio Post Type
add_action('init','wpthesis_create_portfolio_init');
function wpthesis_create_portfolio_init() {
$labels = array
( 'name' => _x('Portfolio', 'post type general name'),
'singular_name' => _x('item', 'post type singular name'),
'add_new' => _x('Add New', 'Portfolio Item'),
'add_new_item' => __('Add New Portfolio Item'),
'edit_item' => __('Edit Portfolio Item'),
'new_item' => __('New Portfolio Item'),
'view_item' => __('View Portfolio Item'),
'search_items' => __('Search Portfolio'),
'not_found' => __('No portfolio items found'),
'not_found_in_trash' => __('No portfolio items found in Trash'),
'parent_item_colon' => ''
);
$support = array
(
'title',
'editor',
'author',
'thumbnail',
'custom-fields',
'comments',
'genesis-seo',
'genesis-layouts',
'revisions'
);
$args = array
(
'labels' => $labels,
'public' => TRUE,
'rewrite' => array('slug'=>('produkte-leistungen'),'with_front'=>false),
'capability_type' => 'page',
'hierarchical' => FALSE,
'query_var' => true,
'supports' => $support, 'taxonomies' => array('portfolio-category'),
'menu_position' => 5
);
register_post_type('portfolio',$args);
register_taxonomy(
'portfolio-category',
'portfolio',
array(
'hierarchical' => TRUE,
'label' => 'Categories',
'query_var' => TRUE,
'rewrite' => FALSE,
)
);
}
i use this code for a custom post type calles portfolio. Now i want to use a parent-child realtion between the items. For example i have a portfolio item with this URL: www.domain.de/portfolio-item1
Another Portfolio Item should be a child from the portfolio item1 and the url should be
www.domain.de/portfolio-item1/portfolio-item2
Im a beginner with php and i dont know how i can realise this. Can somebody help me?
// Portfolio Post Type
add_action('init','wpthesis_create_portfolio_init');
function wpthesis_create_portfolio_init() {
$labels = array
( 'name' => _x('Portfolio', 'post type general name'),
'singular_name' => _x('item', 'post type singular name'),
'add_new' => _x('Add New', 'Portfolio Item'),
'add_new_item' => __('Add New Portfolio Item'),
'edit_item' => __('Edit Portfolio Item'),
'new_item' => __('New Portfolio Item'),
'view_item' => __('View Portfolio Item'),
'search_items' => __('Search Portfolio'),
'not_found' => __('No portfolio items found'),
'not_found_in_trash' => __('No portfolio items found in Trash'),
'parent_item_colon' => ''
);
$support = array
(
'title',
'editor',
'author',
'thumbnail',
'custom-fields',
'comments',
'genesis-seo',
'genesis-layouts',
'revisions'
);
$args = array
(
'labels' => $labels,
'public' => TRUE,
'rewrite' => array('slug'=>('produkte-leistungen'),'with_front'=>false),
'capability_type' => 'page',
'hierarchical' => FALSE,
'query_var' => true,
'supports' => $support, 'taxonomies' => array('portfolio-category'),
'menu_position' => 5
);
register_post_type('portfolio',$args);
register_taxonomy(
'portfolio-category',
'portfolio',
array(
'hierarchical' => TRUE,
'label' => 'Categories',
'query_var' => TRUE,
'rewrite' => FALSE,
)
);
}
Share
Improve this question
edited Oct 19, 2015 at 12:54
Mayeenul Islam
12.9k21 gold badges85 silver badges169 bronze badges
asked Oct 19, 2015 at 12:52
tom84tom84
2551 gold badge6 silver badges21 bronze badges
3
|
1 Answer
Reset to default 2To elaborate on what Mayeenul Islam said, from the WP Docs on regsiter_post_type
hierarchical
(boolean) (optional) Whether the post type is hierarchical (e.g. page). Allows Parent to be specified. The 'supports' parameter should contain 'page-attributes' to show the parent select box on the editor page. Default: false
Emphasis mine. After setting that to true, you should be able to associate the posts with each other.
EDIT: Code added
$support = array (
'title',
'editor',
'author',
'thumbnail',
'custom-fields',
'comments',
'genesis-seo',
'genesis-layouts',
'revisions',
// Add this to supports
'page-attributes',
);
$args = array (
'labels' => $labels,
'public' => TRUE,
'rewrite' => array('slug'=>('produkte-leistungen'),'with_front'=>false),
'capability_type' => 'page',
// You had this set to FALSE, try it with TRUE
'hierarchical' => TRUE,
'query_var' => true,
'supports' => $support,
'taxonomies' => array('portfolio-category'),
'menu_position' => 5
);
register_post_type('portfolio',$args);
In the above snippet, I added 'page-attributes' to $support
and set the 'hierarchical' option to TRUE
in $args
. That should get you set up properly.
本文标签: Parent and Child relation for custom post types
版权声明:本文标题:Parent and Child relation for custom post types 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744766824a2624091.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
'hierarchical' => FALSE,
totrue
. :) – Mayeenul Islam Commented Oct 19, 2015 at 12:54