Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

That i would like (for taxonomy page):

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

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 );
share|improve this question
    
If my answer doesn't solve your purpose let me know. The same worked for me. cheers –  Md Toufiqul Islam Jul 1 '13 at 11:40

1 Answer 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. :)

share|improve this answer
1  
Thanks for your answer, 'hierarchical' work on my URL, but this link : 'parent/children' redirect me to 'parent/singlepost'. Do you know why ? –  PixelBBW Jul 1 '13 at 11:42
    
When you browse the post it will show that way. When you browse child category / taxonomy url you will see your desired url structure. Have fun :) –  Md Toufiqul Islam Jul 2 '13 at 8:46
    
You may accept the answer if you feel it is ok. you know it will give some points for both of us. –  Md Toufiqul Islam Jul 2 '13 at 8:48
    
Oh, I remembered one thing just now, You can set the permalink structure from wp-admin dashboar settings>permalinks. From there select postname option which will serve you best for your url structure. After that clear your browser cache then revisit the site. I hope that will work. –  Md Toufiqul Islam Jul 2 '13 at 9:21
    
Hi, If i hover on taxonomy link, it was good (but not when i click it): Current URL : myurl.com/commerce/artisans/alimentation/boucherie/ When i click : myurl.com/commerce/artisans/alimentation/singlepost/ What i try to do for single post URL : myurl.com/commerce/artisans/alimentation/boucherie/singlepost/ Alimentation is the parent of Boucherie. –  PixelBBW Jul 3 '13 at 6:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.