web-dev-qa-db-fra.com

Réécriture d'URL de type de message personnalisé et de taxonomie

Je crée un thème personnalisé pour mon site web personnel avec Wordpress 3.5.1. J'ai un problème avec le type de message personnalisé WordPress et la taxonomie, voici le détail:

  • nom du type de message personnalisé: articles
  • nom taxonomie: article-categories
  • réécrire slug: articles (idem avec mon cpt)

Actuellement, mon URL est comme ceci:

  • example.com/articles -> archive-articles.php (ça marche)

  • example.com/articles/taxo-parent -> taxonomy.php (ça marche)

  • example.com/articles/taxo-child -> taxonomy.php (ça marche)

  • example.com/articles/post-title -> J'utilise single-articles.php, cela ne fonctionne pas

Je veux que mon URL soit comme ça:

  • example.com/articles

  • example.com/articles/taxo-parent

  • example.com/articles/taxo-parent/taxo-child

  • example.com/articles/taxo-parent/taxo-child/post-title

Cela fait une semaine que nous travaillons avec ce cas et le post de recherche/lecture de Google mais n’avons toujours pas de chance, apprécions vraiment si vous pouviez m'aider.

Merci

MODIFIER:

J'utilise WP un plug-in d'interface utilisateur de type de message personnalisé pour créer un type de message personnalisé et une taxonomie personnalisée

6
TukangBeling

J'ai utilisé le code de mon soufflet pour le type de poste de vos bijoux, vous pouvez simplement remplacer votre type de poste et votre taxonomie. Il suffit de copier/coller sur votre fichier de fonctions.

<?php

//Register a Jewelry post type.
function jewelry_post_register() {
    $labels = array(
        'name'               => _x( 'Jewelries', 'post type general name', 'twentytwelve' ),
        'singular_name'      => _x( 'Jewelry', 'post type singular name', 'twentytwelve' ),
        'menu_name'          => _x( 'Jewelries', 'admin menu', 'twentytwelve' ),
        'name_admin_bar'     => _x( 'Jewelry', 'add new on admin bar', 'twentytwelve' ),
        'add_new'            => _x( 'Add New', 'Jewelry', 'twentytwelve' ),
        'add_new_item'       => __( 'Add New Jewelry', 'twentytwelve' ),
        'new_item'           => __( 'New Jewelry', 'twentytwelve' ),
        'edit_item'          => __( 'Edit Jewelry', 'twentytwelve' ),
        'view_item'          => __( 'View Jewelry', 'twentytwelve' ),
        'all_items'          => __( 'All Jewelries', 'twentytwelve' ),
        'search_items'       => __( 'Search Jewelries', 'twentytwelve' ),
        'parent_item_colon'  => __( 'Parent Jewelries:', 'twentytwelve' ),
        'not_found'          => __( 'No Jewelries found.', 'twentytwelve' ),
        'not_found_in_trash' => __( 'No Jewelries found in Trash.', 'twentytwelve' )
    );

    $args = array(
        'labels'             => $labels,
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'jewelries' ),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => null,
        'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'custom-fields' )
    );

    register_post_type( 'jewelry', $args );

}
add_action( 'init', 'jewelry_post_register' );


// create two taxonomies, Categories and writers for the post type "jewelry"
function create_jewelry_taxonomies() {
    // Add new taxonomy, make it hierarchical (like categories)
    $labels = array(
        'name'              => _x( 'Jewelry Categories', 'taxonomy general name' ),
        'singular_name'     => _x( 'Jewelry Category', 'taxonomy singular name' ),
        'search_items'      => __( 'Search Jewelry Categories' ),
        'all_items'         => __( 'All Jewelry Categories' ),
        'parent_item'       => __( 'Parent Jewelry Category' ),
        'parent_item_colon' => __( 'Parent Jewelry Category:' ),
        'edit_item'         => __( 'Edit Jewelry Category' ),
        'update_item'       => __( 'Update Jewelry Category' ),
        'add_new_item'      => __( 'Add New Jewelry Category' ),
        'new_item_name'     => __( 'New Jewelry Category Name' ),
        'menu_name'         => __( 'Jewelry Category' ),
    );

    $args = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'jewelry-category' ),
    );

    register_taxonomy( 'jewelry-category', array( 'jewelry' ), $args );

}
add_action( 'init', 'create_jewelry_taxonomies', 0 );



function my_custom_template_include( $template ) {
    if ( get_query_var('post_type') == 'jewelry' ) {

        if ( is_single() ) {
            if ( $single = locate_template( array( 'single-jewelry.php') ) )
                return $single;
        }

    }
    return $template;
}

add_filter( 'template_include', 'my_custom_template_include' );
add_filter( 'template_include', 'insert_my_template' );

function insert_my_template( $template )
{
    if ( 'jewelry' === get_post_type() && !is_single() )
        return dirname( __FILE__ ) . '/archive-jewelry.php';


    return $template;
}


?>

Cela fonctionne bien mon site en fonction de votre question.

1
csehasib

Vous ne pouvez pas nommer custom post_type name et taxonomy slug de la même façon, car lorsque vous travaillez sur la taxonomie, cela fonctionne comme suit:

example.com/articles/taxo-parent --> taxonomy.php

example.com/articles/taxo-child --> taxonomy.php

Mais pour les articles post_type, il ira avec la même URL

example.com/post_type/post_in_that_post_type_slug

Donc, cela crée un conflit dans les URL de taxonomy et post_type car l'URL initiale est la même pour les deux

example.com/post_type/ = example.com/articles/

example.com/taxonomy/ = example.com/articles/
1
ksr89

Vous devez mettre à jour votre option permalien après avoir enregistré le type de publication.

http://codex.wordpress.org/Function_Reference/flush_rewrite_rules

Alors ça devrait marcher.

0
Fatih Toprak