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.

I turn to you because I can not find any solution to my problem.

I created 3 custom post types "Actualités", "Dossiers", "Vidéos" (I'm french :D)

I created 1 taxonomy: "Thématiques" with the following terms:

  • Santé
  • Géopolitique
  • ...

And I would like to get links like this (see below - example with CPT: "Dossiers" and tax: "Santé") :

  • www.url.com/dossiers/sante/nom-de-larticle => Display the post (single)
  • www.url.com/dossiers/sante/ => Display posts of post type "Dossiers" with tax "Santé" (archive)
  • www.url.com/dossiers/ => Display all posts of post type "Dossiers" (archive)
  • www.url.com/sante/ => Display all posts of taxonomy "Santé" (archive)

And the same thing for "Videos" and "Actualités".

For now I achieve this with non rewritten urls:

  • www.url.com/?dossiers=nom-de-larticle
  • www.url.com/?post_type=dossiers&thematiques=sante
  • www.url.com/?post_type=dossiers
  • www.url.com/?thematiques=sante

How can I achieve this by manipulating my functions.php? Or adding rewrite rules?

This is my code for Custom Post-Type "Dossiers" (I done the same for "Videos" and "Actualités") :

function register_cpt_dossiers() {
    $labels = array(
        'name' => _x( 'Dossiers', 'dossiers' ),
        'singular_name' => _x( 'Dossier', 'dossiers' ),
        'all_items' => _x( 'Tous les dossiers', 'dossiers' ),
        'add_new' => _x( 'Ajouter', 'dossiers' ),
        'add_new_item' => _x( 'Ajouter un nouveau dossier', 'dossiers' ),
        'edit_item' => _x( 'Modifier le dossier', 'dossiers' ),
        'new_item' => _x( 'Nouveau dossier', 'dossiers' ),
        'view_item' => _x( 'Afficher le dossier', 'dossiers' ),
        'search_items' => _x( 'Chercher dans les dossiers', 'dossiers' ),
        'not_found' => _x( 'Aucun dossier trouvé.', 'dossiers' ),
        'not_found_in_trash' => _x( 'Aucun dossier trouvé dans la corbeille.', 'dossiers' ),
        'parent_item_colon' => _x( 'Magazine parent :', 'dossiers' ),
        'menu_name' => _x( 'Dossiers', 'dossiers' ),
    );
    $args = array(
        'labels' => $labels,
        'hierarchical' => true,
        'supports' => array( 'title', 'editor', 'thumbnail', 'custom-fields', 'revisions' ),
        'taxonomies' => array( 'thematiques'),
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 5,
        'show_in_nav_menus' => true,
        'publicly_queryable' => true,
        'exclude_from_search' => false,
        'has_archive' => 'dossiers',
        'query_var' => true,
        'can_export' => true,
        'rewrite' => array( 'slug' => 'dossiers/%thematiques%', 'hierarchical' => true ),
        'capability_type' => 'post'
    );
    register_post_type( 'dossiers', $args );
}

And this is my code for the Taxonomy "Thématiques" :

register_taxonomy(
    'thematiques',
    array( 'post', 'dossiers','videos' ),
    array(
    'rewrite' => array( 'slug' => 'dossiers' ),
    'label' => 'Thématiques',
    'query_var' => true
    )
);

And the function for permalinks :

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

Thank you guys !

share|improve this question
    
See this answer for how to get a taxonomy term in a cpt permalink. your desired url for taxonomy term www.url.com/sante/ will clash with page rewrite rules and cause them to 404, it's far simpler to have www.url.com/thematiques/sante/. –  Milo Aug 2 '13 at 0:50
    
I did exactly what you said in the answer but it works only for www.url.com/dossiers/sante/nom-de-larticle. Thank you Milo for your help –  Ryad Karar Aug 2 '13 at 1:12
    
I added "theme" to www.url.com/sante/ so now it's www.url.com/theme/sante/ and it show good but not the rest => www.url.com/dossiers/sante/ and www.url.com/dossiers/ –  Ryad Karar Aug 2 '13 at 1:21
    
update your question with your current code. –  Milo Aug 2 '13 at 2:22
    
Code updated! Thanks –  Ryad Karar Aug 2 '13 at 2:45
show 7 more comments

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.