0

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,
    )
  );

1 Answer 1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.