WordPress.org

Ready to get started?Download WordPress

Forums

Adding rewrite rules to Custom Post-type mixed with Taxonomy (2 posts)

  1. ryudr
    Member
    Posted 11 months ago #

    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é") :

    http://www.url.com/dossiers/sante/nom-de-larticle => Display the post (single)
    http://www.url.com/dossiers/sante/ => Display posts of post type "Dossiers" with tax "Santé" (archive)
    http://www.url.com/dossiers/ => Display all posts of post type "Dossiers" (archive)
    http://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:

    http://www.url.com/?dossiers=nom-de-larticle
    http://www.url.com/?post_type=dossiers&thematiques=sante
    http://www.url.com/?post_type=dossiers
    http://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' => true,
        'query_var' => true,
        'can_export' => true,
        'rewrite' => true,
        'capability_type' => 'post'
    );
    register_post_type( 'dossiers', $args );

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

    register_taxonomy(
        'thematiques',
        array( 'actualites', 'dossiers','videos' ),
        array( 'hierarchical' => true, 'label' => 'Thématiques', 'query_var' => true, 'rewrite' => true )
    );
  2. ryudr
    Member
    Posted 11 months ago #

    Resolved by buying this plugin: http://www.gdcpttools.com/features/

Reply

You must log in to post.

About this Topic