1

We are currently developing a new wp theme and all the current posts are moved to a new CPT called products. For SEO the links to the products need to be remained.

The current structure of the post type is:

www.url.com/category-a/category-b/productname

After moving the items from the default post type to the new CPT the URL looks like:

www.url.com/products/productname

But we want to remain the default post structure for these posts. The archive pages show the products, so that’s working fine.

My permalink setting is:

/%category%/%pagename%

So basicly we want to change: www.url.com/products/productname

To: www.url.com/category-a/subcategory-a/productname

This is our CPT we created:

function cptui_register_my_cpts_producten() {

    /**
     * Post Type: Producten.
     */

    $labels = array(
        "name" => __( "Producten", "sage" ),
        "singular_name" => __( "Product", "sage" ),
        "archives" => __( "Producten Archief", "sage" ),
    );

    $args = array(
        "label" => __( "Producten", "sage" ),
        "labels" => $labels,
        "description" => "",
        "public" => true,
        "publicly_queryable" => true,
        "show_ui" => true,
        "show_in_rest" => false,
        "rest_base" => "",
        "has_archive" => false,
        "show_in_menu" => true,
        "show_in_nav_menus" => true,
        "exclude_from_search" => false,
        "capability_type" => "post",
        "map_meta_cap" => true,
        "hierarchical" => true,
        "rewrite" => array( "slug" => "producten", "with_front" => false ),
        "query_var" => true,
        "supports" => array( "title", "editor", "thumbnail" ),
        "taxonomies" => array( "category" ),
    );

    register_post_type( "producten", $args );
}

add_action( 'init', 'cptui_register_my_cpts_producten' );

1 Answer 1

0

You have to set "hierarchical" => true in order to get %category% and %subcategory% to display:

"rewrite" => array(
    "slug" => "producten",
    "with_front" => false,
    "hierarchical" => true
),

Don't forget to flush permalink settings by opening settingspermalinks and save.

More info: WordPress Codex: Function Reference/register post type

3
  • Hmm, I'm afraid that's not what the question was about... 'category' is a built-in taxonomy and it is hierarchical. So nobody is registering any taxonomy in here... Commented Jun 23, 2018 at 7:56
  • I linked the wrong article. But still: I think he is trying to register a custom post type and wants its categories to display in the permalink structure, so the above code should work.
    – nazim
    Commented Jun 23, 2018 at 8:25
  • yes, he’s trying to register CPT and put categories in its URLs. Your code does nothing like that - it only makes that CPT hierarchical - it has nothing to do with categories or any other taxonomy... Commented Jun 23, 2018 at 10:11

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.