Get term name and term parent into custom post type permalink

That i would like (for taxonomy page):

Current URL : myurl.com/commerce/artisans/boucherie/

Read More

What i try to do URL :myurl.com/commerce/artisans/alimentation/boucherie/

Alimentation is the parent of Boucherie.

That i would like (for single page) :

Current URL : myurl.com/commerce/artisans/alimentation/singlepost

What i try to do URL : myurl.com/commerce/artisans/alimentation/boucherie/singlepost

Alimentation is the parent of Boucherie.

Thank you for your help.

This is my code:



//This code tells WordPress when to run our setup function
add_action('after_setup_theme', 'map');

function map(){

//setup slider post type
//Set the names of the labels for the Post type interface
 $post_labels = array(
        'name' => 'Carte',
        'singular_name' => 'Carte',
        'add_new' => 'Ajouter un point carte',
        'add_new_item' => 'Ajouter un point carte',
        'edit_item' => 'Editer un point carte',
        'new_item' => 'Nouveau point carte',
        'view_item' => 'Afficher un point carte',
        'search_items' => 'Rechercher un point carte',
        'not_found' =>  'Aucun point carte',
        'not_found_in_trash' => 'Aucun point carte trouvés dans la corbeille',
        'parent_item_colon' => ''
    );


//Setup the rules for this post type. You can read more about what each of these do in
//the WP codex under register_post_type().
    $post_args = array(
        'labels' => $post_labels, //Add the labels that we set above
        'public' => true,
        'publicly_queryable' => true,//hide this post type from being accessed directly
        'show_ui' => true,
        'show_in_menu' => true,
        'query_var' => true,
        'rewrite' => array('slug'=>'commerce/artisans/%map_categories%','with_front'=>false),
        'capability_type' => 'post',
        'hierarchical' => false,
        'supports' => array('title','editor' ), //specify what items the post type supports.
        'has_archive' => false,
        'menu_icon' => get_stylesheet_directory_uri() . '/map.png',
    );
    //register the post type using the arguments we have setup above.
    register_post_type('map',$post_args);
    register_taxonomy( 'map_categories', 'map', array( 'hierarchical' => true, 'label' => 'Catégories', 'query_var' => true, 'rewrite' => array('slug'=>'commerce/artisans','with_front'=>false) ) );

}

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

Related posts

1 comment

  1. I solved this issue by the following codes where you need to define “rewrite” argument as follows

    register_taxonomy( 'map_categories', 'map', array( 'hierarchical' => true, 'label' => 'Catégories', 'query_var' => true, 'rewrite' => array('slug'=>'commerce/artisans','with_front'=>false, 'hierarchical' => true) ) );
    

    You need to understand that ‘hierarchical’ in rewrite actually solve your purpose.

    After the update in codes you need to save your permalink structure again. 🙂

Comments are closed.