how to make custom post type permalinks in to custom-post-type/parent-category/child-category/post-slug

Question

Hi I am having trouble producing my permalink on my custom post type into this

website-sample.com/custom-post-type/parent-category/child-category/post-slug

Here is my custom post type code under products-post-type.php

<?php
/*
** ----------
** Register Products Taxonomy - Hierarchical
** ----------
*/

function products_taxonomy_hierarchical() {

    $labels = array(
        'name'                          => _x( 'Brand Categories', 'Taxonomy General Name', 'text_domain' ),
        'singular_name'                 => _x( 'Brand Category', 'Taxonomy Singular Name', 'text_domain' ),
        'menu_name'                     => __( 'Brand Categories', 'text_domain' ),
        'all_items'                     => __( 'All Brand Categories', 'text_domain' ),
        'parent_item'                   => __( 'Parent Brand Category', 'text_domain' ),
        'parent_item_colon'             => __( 'Parent brand category:', 'text_domain' ),
        'new_item_name'                 => __( 'New Brand Category', 'text_domain' ),
        'add_new_item'                  => __( 'Add New Brand Category', 'text_domain' ),
        'edit_item'                     => __( 'Edit Brand Category', 'text_domain' ),
        'update_item'                   => __( 'Update Brand Category', 'text_domain' ),
        'view_item'                     => __( 'View Brand Category', 'text_domain' ),
        'separate_items_with_commas'    => __( 'Separate brand categories with commas', 'text_domain' ),
        'add_or_remove_items'           => __( 'Add or remove brand categories', 'text_domain' ),
        'choose_from_most_used'         => __( 'Choose from the most used', 'text_domain' ),
        'popular_items'                 => __( 'Popular Brand Categories', 'text_domain' ),
        'search_items'                  => __( 'Search Brand Categories', 'text_domain' ),
        'not_found'                     => __( 'Not Found', 'text_domain' ),
        'no_terms'                      => __( 'No items', 'text_domain' ),
        'items_list'                    => __( 'Brand Categories list', 'text_domain' ),
        'items_list_navigation'         => __( 'Brand Categories list navigation', 'text_domain' ),
    );

    $args = array(
        'labels'                        => $labels,
        'hierarchical'                  => true,
        'public'                        => true,
        'show_ui'                       => true,
        'query_var'                     => true,
        'show_admin_column'             => true,
        'show_in_nav_menus'             => true,
        'show_in_rest'                  => true,
        'rewrite'                       => array(
                                                    'slug' => 'brands/categories',
                                                    'with_front' => true,
                                                    'hierarchical'   => true,
                                                                         ),
    );
    register_taxonomy( 'product-categories', array( 'products' ), $args );

}
add_action( 'init', 'products_taxonomy_hierarchical', 0 );





/*
** ----------
** Register Products Post Type
** ----------
*/
function products_post_type() {

    $labels = array(
        'name'                          => _x( 'Brands', 'Post Type General Name', 'text_domain' ),
        'singular_name'                 => _x( 'Product', 'Post Type Singular Name', 'text_domain' ),
        'menu_name'                     => __( 'Products', 'text_domain' ),
        'name_admin_bar'                => __( 'Products', 'text_domain' ),
        'archives'                      => __( 'Products', 'text_domain' ),
        'attributes'                    => __( 'Product Attributes', 'text_domain' ),
        'parent_item_colon'             => __( 'Parent Product:', 'text_domain' ),
        'all_items'                     => __( 'All Products', 'text_domain' ),
        'add_new_item'                  => __( 'Add New Product', 'text_domain' ),
        'add_new'                       => __( 'Add New Product', 'text_domain' ),
        'new_item'                      => __( 'New Product', 'text_domain' ),
        'edit_item'                     => __( 'Edit Product', 'text_domain' ),
        'update_item'                   => __( 'Update Product', 'text_domain' ),
        'view_item'                     => __( 'View Product', 'text_domain' ),
        'view_items'                    => __( 'View Products', 'text_domain' ),
        'search_items'                  => __( 'Search Product', 'text_domain' ),
        'not_found'                     => __( 'Not found', 'text_domain' ),
        'not_found_in_trash'            => __( 'Not found in Trash', 'text_domain' ),
        'featured_image'                => __( 'Featured Image', 'text_domain' ),
        'set_featured_image'            => __( 'Set featured image', 'text_domain' ),
        'remove_featured_image'         => __( 'Remove featured image', 'text_domain' ),
        'use_featured_image'            => __( 'Use as featured image', 'text_domain' ),
        'insert_into_item'              => __( 'Insert into product', 'text_domain' ),
        'uploaded_to_this_item'         => __( 'Uploaded to this product', 'text_domain' ),
        'items_list'                    => __( 'Products list', 'text_domain' ),
        'items_list_navigation'         => __( 'Products list navigation', 'text_domain' ),
        'filter_items_list'             => __( 'Filter products list', 'text_domain' ),
    );

    $args = array(
        'label'                         => __( 'Product', 'text_domain' ),
        'labels'                        => $labels,
        'supports'                      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments'),
        'taxonomies'                    => array( 'product-categories' ),
        'hierarchical'                  => false,
        'public'                        => true,
        'show_ui'                       => true,
        'show_in_menu'                  => true,
        'menu_position'                 => 5,
        'menu_icon'                     => 'dashicons-screenoptions',
        'show_in_admin_bar'             => true,
        'show_in_nav_menus'             => true,
        'query_var'                     => true,
        'can_export'                    => true,
        'has_archive'                   => true,
        'exclude_from_search'           => false,
        'publicly_queryable'            => true,
        'show_in_rest'                  => true,
        'capability_type'               => 'post', //...or page
        'rewrite'                       => array(
                                            'slug'  =>  'brands/%product-categories%',
                                            'with_front' => true,
                                            'hierarchical' => true,
                                        ),
    );
    register_post_type( 'products', $args );

}
add_action( 'init', 'products_post_type', 0 );

?>

right now i am using a function code that I got also here and only prints one category on each post. This is under functions.php

function wpse_5308_post_type_link( $link, $post ) {
    if ( $post->post_type === 'products' ) {
        if ( $terms = get_the_terms( $post->ID, 'product-categories' ) )
                    $link = str_replace( '%product-categories%', current( $terms )->slug, $link );
            else
                    $link = str_replace( '%product-categories%', 'uncategorized', $link );
        }
    
    return $link;

}

add_filter( 'post_type_link', 'wpse_5308_post_type_link', 10, 2 );

Anyone one here that can help me with my problem. Badly need the answer for this. It’s almost 1 week now and I can’t get the right function for this. Thanks guys!

0
Kriz Laurente 4 weeks 2021-04-12T21:57:26-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse