Viewing 3 replies - 1 through 3 (of 3 total)
  • You can use the register_taxonomy function to register a custom taxonomy for your post type, and then use the rewrite parameter to set the desired permalink structure. Here’s an example:

    register_taxonomy(
        'custom_taxonomy', 
        'custom_post_type', 
        array(
            'hierarchical' => true,
            'label' => 'Custom Taxonomy',
            'query_var' => true,
            'rewrite' => array(
                'slug' => 'ca-en/%custom_taxonomy_parent%/%custom_taxonomy%',
                'with_front' => false
            )
        )
    );

    In the above example, %custom_taxonomy_parent% and %custom_taxonomy% are placeholders that will be replaced with the actual parent and child categories when the permalink is generated. The with_front parameter is set to false to remove the front base of the URL.

    Additionally, you need to add filter post_type_link to modify the generated link.

    function custom_post_type_link( $post_link, $id = 0, $leavename ) {
        $post = get_post($id);
        if ( is_object( $post ) && $post->post_type == 'custom_post_type' ){
            $terms = wp_get_object_terms( $post->ID, 'custom_taxonomy' );
            if( $terms ){
                return str_replace( '%custom_taxonomy%' , $terms[0]->slug , $post_link );
            }
        }
        return $post_link;
    }
    
    add_filter( 'post_type_link', 'custom_post_type_link', 1, 3 );

    This is just an example, you may need to adjust the code to match your specific requirements.

    Thread Starter nate.ads

    (@nateads)

    Thank you for your reply @faisalahammad, unfortunately I am doing something wrong but I cannot figure out where.

    Below is all my code for the CPT

    function brochure_post() {
    
        $labels = array(
    
            'name'               => _x( 'Brochure Product', 'post type general name' ),
    
            'singular_name'      => _x( 'Brochure Product', 'post type singular name' ),
    
            'add_new'            => _x( 'Add New', 'product' ),
    
            'add_new_item'       => __( 'Add New Brochure' ),
    
            'edit_item'          => __( 'Edit Brochure' ),
    
            'new_item'           => __( 'New Brochure' ),
    
            'all_items'          => __( 'All Brochures' ),
    
            'view_item'          => __( 'View Brochure' ),
    
            'search_items'       => __( 'Search Brochures' ),
    
            'not_found'          => __( 'No Brochure found' ),
    
            'not_found_in_trash' => __( 'No Brochure found in the Trash' ),
    
            'parent_item_colon'  => '',
    
            'menu_name'          => 'Brochures',
    
        );
    
        $args = array(
    
            'labels'        => $labels,
    
            'description'   => 'Holds Brochure and Brochure specific data',
    
            'public'        => true,
    
            'show_ui'       => true,
    
            'menu_position' => 5,
    
            'supports'      => array( 'title', 'editor', 'thumbnail', 'category', 'tags', 'page-attributes' ),
    
            'has_archive'   => false,
    
            'taxonomies'    => array( 'category' ),
    
        );
    
        register_post_type( 'brochures', $args );
    
    }
    
    add_action( 'init', 'brochure_post', 0 );
    
    register_taxonomy( 'custom_taxonomy', 'custom_post_type', array(
    
        'hierarchical' => true,
    
        'label' => 'Custom Taxonomy',
    
        'query_var' => true,
    
        'rewrite' => array(
    
            'slug' => '/%custom_taxonomy_parent%/%custom_taxonomy%',
    
            'with_front' => false,
    
        )
    
    )
    
    );
    
    function custom_post_type_link( $post_link, $id = 0, $leavename ) {
    
    $post = get_post($id);
    
    if ( is_object( $post ) && $post->post_type == 'custom_post_type' ) {
    
        $terms = wp_get_object_terms( $post->ID, 'custom_taxonomy' );
    
        if( $terms ) {
    
            return str_replace( '%custom_taxonomy%' , $terms[0]->slug , $post_link );
    
        }
    
    }
    
    return $post_link;
    
    }
    
    add_filter( 'post_type_link', 'custom_post_type_link', 1, 3 );
    Thread Starter nate.ads

    (@nateads)

    Problem solved!

    I used the below code to create a CPT and it has the ability to rewrite a part of the URL in the browser address bar, and depending on the categories used in the Dashboard will display in the address bar.

    function brochure_post() {
    
        $labels = array(
    
            'name'               => _x( 'Brochure Product', 'post type general name' ),
    
            'singular_name'      => _x( 'Brochure Product', 'post type singular name' ),
    
            'add_new'            => _x( 'Add New', 'product' ),
    
            'add_new_item'       => __( 'Add New Brochure' ),
    
            'edit_item'          => __( 'Edit Brochure' ),
    
            'new_item'           => __( 'New Brochure' ),
    
            'all_items'          => __( 'All Brochures' ),
    
            'view_item'          => __( 'View Brochure' ),
    
            'search_items'       => __( 'Search Brochures' ),
    
            'not_found'          => __( 'No Brochure found' ),
    
            'not_found_in_trash' => __( 'No Brochure found in the Trash' ),
    
            'parent_item_colon'  => '',
    
            'menu_name'          => 'Brochures',
    
        );
    
        $args = array(
    
            'labels'        => $labels,
    
            'description'   => 'Holds Brochure and Brochure specific data',
    
            'public'        => true,
    
            'show_ui'       => true,
    
            'menu_position' => 5,
    
            'supports'      => array( 'title', 'editor', 'thumbnail', 'category', 'tags', 'page-attributes' ),
    
            'has_archive'   => false,
    
            'taxonomies'    => array( 'category' ),
    
            'rewrite'       => array( 'slug' => 'products/%category%', 'with_front' => false ),
    
        );
    
        register_post_type( 'brochures', $args );
    
    }
    
    add_action( 'init', 'brochure_post', 0 );
    
    function brochure_permalink( $post_link, $id = 0 ){
    
        $post = get_post($id);  
    
            $terms = wp_get_object_terms( $post->ID, 'category' );
    
            if( $terms ){
    
                return str_replace( '%category%' , $terms[0]->slug , $post_link );
    
        } else {
    
            return str_replace( '%category%/' , '' , $post_link );
    
    }
    
        return $post_link;  
    
    }
    
    add_filter( 'post_type_link', 'brochure_permalink', 1, 3 );
    • This reply was modified 3 years, 4 months ago by nate.ads.
Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘CPT Permalink (parent/child)’ is closed to new replies.