Custom Post Type and Taxonomy URL rewrite

I’m creating custom theme for my personal website using WordPress 3.5.1. I have problem with the WordPress custom post type and taxonomy, here’s the detail:

  • custom post type name: articles
  • taxonomy name: article-categories
  • rewrite slug: articles (same with my cpt)

Currently my URL is like this:

Read More
  • example.com/articles –> archive-articles.php (it works)

  • example.com/articles/taxo-parent –> taxonomy.php (it works)

  • example.com/articles/taxo-child –> taxonomy.php (it works)

  • example.com/articles/post-title –> I use single-articles.php, it doesn’t work

I want my URL to be like this:

  • example.com/articles

  • example.com/articles/taxo-parent

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

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

It’s been a week working with this case and search / reading post from google but still have no luck, really appreciate if you guys could help me.

Thanks

EDIT:

I use WP Custom Post Type UI plugin to create custom post type and custom taxonomy

Related posts

3 comments

  1. I have used my jewelery post type bellows code, You can just replace your post type & taxonomy. Just copy & paste on your functions file.

    <?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;
    }
    
    
    ?>
    

    It works nicely my site according to your question.

  2. You can not name custom post_type name and taxonomy slug same because when you work on taxonomy it will work as

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

    But for post_type articles it will go with same url

    example.com/post_type/post_in_that_post_type_slug
    

    So this make conflict in the url’s of taxonomy and post_type because initial url is same for both of them

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

Comments are closed.