0

I created custom taxonomy(province) I want add custom taxonomy after post category and before post name in wordpress permalink

I want add custom taxonomy to wordpress default post type

for example : https://example.com/category/province/post

My Code:

 $args = array(
    'hierarchical'      => true,
    'labels'            => $labels,
    'show_ui'           => true,
    'show_admin_column' => true,
    'query_var'         => true,
    'rewrite'           => array( 'slug' => 'province' ,  'with_front' => false ),
);
register_taxonomy( 'province', array( 'post' ), $args );


function add_custom_to_permalinks( $post_link, $post ){
if ( is_object( $post ) && $post->post_type == 'post' ){
    $terms = wp_get_object_terms( $post->ID, 'province' );
    if( $terms ){
        return str_replace( '%province%' , $terms[0]->slug , $post_link );
    }
}
return $post_link;
}
add_filter( 'post_type_link', 'add_custom_to_permalinks', 1, 2 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.