web-dev-qa-db-fra.com

Règles de réécriture de WordPress pour le type de message personnalisé et la taxonomie

J'ai trouvé que cet endroit était une bonne source d'informations dans le passé grâce à de nombreuses recherches sur Google pour résoudre les problèmes que j'ai rencontrés. Ma question concerne les règles de réécriture verbeuses utilisées par WordPress.

J'ai configuré un type de message personnalisé appelé projet et j'ai enregistré une taxonomie personnalisée appelée projets . Tout fonctionne très bien, à l'exception des options de réécriture des slug, qui finissent par être en conflit, probablement en raison des règles de réécriture.

En gros, voici la structure que je cherche à atteindre:

  • example.com/work/%taxonomy%/%post_name%/ (pour les articles)
  • example.com/work/%taxonomy%/ (liste des publications appartenant à un terme de taxonomie particulier)
  • example.com/work/ (va à page-work.php qui inclut taxonomy.php pour lister tous les articles associés à cette taxonomie)

Voici le code que j’ai jusqu’à présent, mais j’ai besoin d’aide pour écrire les règles WP_Rewrite car c’est le bit qui m’arrête un peu.

$labels = array(
    'name' => _x('Projects', 'post type general name'),
    'singular_name' => _x('Project', 'post type singular name'),
    'add_new' => _x('Add New', 'project item'),
    'add_new_item' => __('Add New Project'),
    'edit_item' => __('Edit Project'),
    'new_item' => __('New Project'),
    'view_item' => __('View Project'),
    'search_items' => __('Search Projects'),
    'not_found' =>  __('Nothing found'),
    'not_found_in_trash' => __('Nothing found in Trash'),
    'parent_item_colon' => ''
);

$args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'hierarchical' => true,
    'rewrite' => array('slug'=>'work', 'with_front'=>false),
    'show_ui' => true,
    '_builtin' => false, // It's a custom post type, not built in!
    'capability_type' => 'post',
    'query_var' => "project", // This goes to the WP_Query schema
    'menu_position' => null,
    'supports' => array('title','editor','thumbnail', 'comments', 'author', 'excerpt')
);

register_post_type('project' , $args);

// Showcase Taxonomy
register_taxonomy('projects', array('project'), array(
    'public' => true,
    'hierarchical' => true,
    'label' => 'Project Categories', 
    'singular_label' => 'Project Category',
    'query_var' => true,
    'rewrite' => array('slug'=>'work', 'with_front'=>false, 'hierarchical'=>true)
    )
);

Merci beaucoup pour votre aide! :-)

9
matt_d_rat

J'ai eu le même problème et après beaucoup de difficultés, j'ai trouvé cette solution.
Ajoutez simplement ceci à votre code

global $wp_rewrite;
$wp_rewrite->flush_rules(); 

function my_custom_post_type() {
    $labels = array(
        'name' => _x('Projects', 'post type general name'),
        'singular_name' => _x('Project', 'post type singular name'),
        'add_new' => _x('Add New', 'project item'),
        'add_new_item' => __('Add New Project'),
        'edit_item' => __('Edit Project'),
        'new_item' => __('New Project'),
        'view_item' => __('View Project'),
        'search_items' => __('Search Projects'),
        'not_found' =>  __('Nothing found'),
        'not_found_in_trash' => __('Nothing found in Trash'),
        'parent_item_colon' => '',
        'menu_name' => 'Projects' 
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
            'hierarchical' => false,
            'has_archive' => true,
        'rewrite' => array('slug'=>'work', 'with_front'=>false),
        'show_ui' => true,
        '_builtin' => false, // It's a custom post type, not built in!
        'capability_type' => 'post',
            'query_var' => true, // This goes to the WP_Query schema
        'menu_position' => null,
        'supports' => array('title','editor','thumbnail', 'comments', 'author', 'excerpt')
    );

    register_post_type( 'work' , $args );

    global $wp_rewrite;   
    $wp_rewrite->flush_rules();    // this should help 
}
1
Dipesh KC

J'espère que cela peut résoudre votre problème

function my_custom_post_type() {
$labels = array(
    'name' => _x('Projects', 'post type general name'),
    'singular_name' => _x('Project', 'post type singular name'),
    'add_new' => _x('Add New', 'project item'),
    'add_new_item' => __('Add New Project'),
    'edit_item' => __('Edit Project'),
    'new_item' => __('New Project'),
    'view_item' => __('View Project'),
    'search_items' => __('Search Projects'),
    'not_found' =>  __('Nothing found'),
    'not_found_in_trash' => __('Nothing found in Trash'),
    'parent_item_colon' => '',
    'menu_name' => 'Projects' 
);

$args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
        'hierarchical' => false,
        'has_archive' => true,
    'rewrite' => array('slug'=>'work', 'with_front'=>false),
    'show_ui' => true,
    '_builtin' => false, // It's a custom post type, not built in!
    'capability_type' => 'post',
        'query_var' => true, // This goes to the WP_Query schema
    'menu_position' => null,
    'supports' => array('title','editor','thumbnail', 'comments', 'author', 'excerpt')
);

register_post_type( 'work' , $args );

}
function my_custom_taxonomies() {

    $labels = array(
        'name' => __( 'Taxonomy', 'taxonomy general name' ),
        'singular_name' => __( 'Taxonomy', 'taxonomy singular name' ),
        'search_items' =>  __( 'Search Taxonomy' ),
        'all_items' => __( 'All Taxonomy' ),
        'parent_item' => __( 'Parent Taxonomy' ),
        'parent_item_colon' => __( 'Parent Taxonomy:' ),
        'edit_item' => __( 'Edit Taxonomy' ), 
        'update_item' => __( 'Update Taxonomy' ),
        'add_new_item' => __( 'Add New Taxonomy' ),
        'new_item_name' => __( 'New Taxonomy Name' ),
        'menu_name' => __( 'Taxonomy' ),
    );  

    register_taxonomy( 'taxonomy', array('work'), array (
                    'labels' => $labels,
                    'hierarchical' =>false,
                    'show_ui' => true,
                    'rewrite' => array( 'slug' => 'work/taxonomy'),
                    'query_var' => true,
                    'show_in_nav_menus' => true,
                    'public' => true,
            ));
}

add_action('init', 'my_custom_post_type', 0);
add_action('init', 'my_custom_taxonomies', 10);

ce que vous devez créer est archive-work.php (votre archive de type publication) et taxonomy.php, qui seront utilisés pour afficher votre archive de taxonomie personnalisée.

1
nonsensecreativity

Une explication plus détaillée est sur un autre post , mais voici les éléments de base que vous devez ajouter:

  1. Enregistrez vos taxonomies et cpt comme vous le faites. Assurez-vous que votre slug de réécriture pour le taxo est "nom de base" et que le slug de réécriture pour le cpt est "nom de base /% nom_taxe%".

  2. Dites à wordpress quoi faire avec "% tax_name%" comme ceci:

    function filter_post_type_link($link, $post)
    {
    if ($post->post_type != 'custom_post_type_name')
        return $link;
    
    if ($cats = get_the_terms($post->ID, 'taxonomy_name'))
    {
        $link = str_replace('%taxonomy_name%',array_pop($cats)->term_id, link); // see custom function defined below
    }
    return $link;
    }
    add_filter('post_type_link', 'filter_post_type_link', 10, 2);
    
0
Jeff