admin管理员组文章数量:1135119
General Setup
I'm working on a setup to create a documentary/wiki site for a company. The site should have three different levels of hierarchy and one type of content at the lower end:
- level 1:
area
(high level structure, e.g. 'internal stuff' or 'external projects') - level 2:
topic
(main structure, e.g. 'controlling' or 'project xyz') - level 3:
section
(groups the content, for a project this could be 'project management') - level 4:
content
(custom post type that holds the content, basically like pages)
Issue 1
Ideally I have a custom post type for all four level. But is this even possible? Can I have a custom post type under another custom post type? I didn't find anything about this...
Permalinks
So the permalinks should be like this:
mysite/area/external-projects
Simply lists all topics in this area, so area is kind of a category for topic
mysite/topic/project-xyz
Lists all the sections and the content entries.
Link to section is not needed.
The link to content is tricky and the point where I need help:
mysite/topic/my-topic/content/this-is-my-content
Issue 2
So, the permalinks for the content should include the topic and the content. Is this even possible if the section (the hierarchy between them) is not in the permalink?
General Setup
I'm working on a setup to create a documentary/wiki site for a company. The site should have three different levels of hierarchy and one type of content at the lower end:
- level 1:
area
(high level structure, e.g. 'internal stuff' or 'external projects') - level 2:
topic
(main structure, e.g. 'controlling' or 'project xyz') - level 3:
section
(groups the content, for a project this could be 'project management') - level 4:
content
(custom post type that holds the content, basically like pages)
Issue 1
Ideally I have a custom post type for all four level. But is this even possible? Can I have a custom post type under another custom post type? I didn't find anything about this...
Permalinks
So the permalinks should be like this:
mysite.com/area/external-projects
Simply lists all topics in this area, so area is kind of a category for topic
mysite.com/topic/project-xyz
Lists all the sections and the content entries.
Link to section is not needed.
The link to content is tricky and the point where I need help:
mysite.com/topic/my-topic/content/this-is-my-content
Issue 2
So, the permalinks for the content should include the topic and the content. Is this even possible if the section (the hierarchy between them) is not in the permalink?
Share Improve this question edited Sep 12, 2023 at 20:27 sotirov 1271 silver badge10 bronze badges asked Sep 10, 2023 at 22:02 StephanStephan 11 Answer
Reset to default 0You can do this with custom hierarchical taxonomy and use it for your custom post type.
Register the Taxonomy area
for the post type content
using the init
action hook:
function wporg_register_taxonomy_area() {
$labels = array(
'name' => _x( 'Areas', 'taxonomy general name' ),
'singular_name' => _x( 'Area', 'taxonomy singular name' ),
'search_items' => __( 'Search Areas' ),
'all_items' => __( 'All Areas' ),
'parent_item' => __( 'Parent Area' ),
'parent_item_colon' => __( 'Parent Area:' ),
'edit_item' => __( 'Edit Area' ),
'update_item' => __( 'Update Area' ),
'add_new_item' => __( 'Add New Area' ),
'new_item_name' => __( 'New Area Name' ),
'menu_name' => __( 'Area' ),
);
$args = array(
'hierarchical' => true, // make it hierarchical (like categories)
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => [ 'slug' => 'area' ],
);
register_taxonomy( 'area', [ 'content' ], $args );
}
add_action( 'init', 'wporg_register_taxonomy_area' );
Register custom post type content
using the init
action hook:
function my_custom_content_item() {
$labels = array(
'name' => _x( 'Content', 'post type general name' ),
'singular_name' => _x( 'Content', 'post type singular name' ),
'menu_name' => 'Content'
);
$args = array(
'labels' => $labels,
'public' => true,
'has_archive' => true,
'taxonomies' => array( 'area' ),
'show_in_rest' => true,
);
register_post_type( 'content', $args );
}
add_action( 'init', 'my_custom_content_item' );
Adds the registered taxonomy area
to object type content
.
function attach_area_to_content() {
register_taxonomy_for_object_type( 'area', 'content' );
}
add_action('init','attach_area_to_content');
本文标签: How to have a hierarchy of custom post types and use two of them in the permalink
版权声明:本文标题:How to have a hierarchy of custom post types and use two of them in the permalink? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736807345a1953743.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论