admin管理员组

文章数量:1122846

How to make such a structure for cpt? '/slug/%category%/%category2%/%postname%/' for post '/slug/%category1%/%category2%/' for taxonomy

There will be a maximum of 2 levels of categories. If a post has one category then '/slug/%category%/%postname%/'

I need this kind of structure only for this CPT and Taxonomy.

    register_post_type(
'slug_cpt',
 array(
 'labels'              => $labels,
 'exclude_from_search' => false,
'has_archive'         => true,
'public'              => true,
'publicly_queryable' => true,
'rewrite'  => true,
'can_export'          => true,
'show_in_nav_menus'   => true,
'supports'            => array('title', 'thumbnail', 'comments', 'page-attributes', 'custom-fields'),
'show_in_rest' =>true,
 )
);
register_taxonomy(
'slug_cats',
'slug_cpt',
 array(
 'hierarchical'      => true,
 'show_admin_column' => true,
 'show_in_nav_menus' => true,
 'labels'            =>array(),
 'query_var'         => true,
 'show_in_rest' => true,
    )
  );

How to make such a structure for cpt? '/slug/%category%/%category2%/%postname%/' for post '/slug/%category1%/%category2%/' for taxonomy

There will be a maximum of 2 levels of categories. If a post has one category then '/slug/%category%/%postname%/'

I need this kind of structure only for this CPT and Taxonomy.

    register_post_type(
'slug_cpt',
 array(
 'labels'              => $labels,
 'exclude_from_search' => false,
'has_archive'         => true,
'public'              => true,
'publicly_queryable' => true,
'rewrite'  => true,
'can_export'          => true,
'show_in_nav_menus'   => true,
'supports'            => array('title', 'thumbnail', 'comments', 'page-attributes', 'custom-fields'),
'show_in_rest' =>true,
 )
);
register_taxonomy(
'slug_cats',
'slug_cpt',
 array(
 'hierarchical'      => true,
 'show_admin_column' => true,
 'show_in_nav_menus' => true,
 'labels'            =>array(),
 'query_var'         => true,
 'show_in_rest' => true,
    )
  );
Share Improve this question asked Jun 1, 2024 at 19:20 Сергей КонстантиновСергей Константинов 32 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

If you use the Advanced Custom Fields plugin (ACF) to create your custom taxonomy for your custom post type then you can just check the box to make it hierarchical. It will then automatically create a url structure matching the hierarchy.

本文标签: url rewritingHow to make such a structure for cpt