0

I have a website which has a CPT of products and product categories with subcategories. I want the url sctructure for taxonomy pages and single pages to follow url/products/term/single-product and url/products/term/sub-term

I have a rewrite function set up to display the cpt and term in the url for the single post but when I have the taxonomy rewrite set to the same name the pages to not go to the correct templates or go to a 404.

I know it is not good practice to have the rewrites the same names for cpt and taxonomies but this is what the client is after and this works until you want to go to an archive page of term with a sub term.

I have tried what this post suggests but it errors out and the single-products.php redirects to the single.php but pulls through no data. Custom Post type & Taxonomy URL structure

Any help would be great!

here is the code that I am using

    add_action( 'init', 'product_post_type_func' );
/**
 * Products post type
 */
function product_post_type_func() {
    $labels = array(
        'name'               => __( 'Products' ),
        'singular_name'      => __( 'Product' ),
        'add_new'            => __( 'Add New' ),
        'add_new_item'       => __( 'Add New Product' ),
        'edit_item'          => __( 'Edit Product' ),
        'new_item'           => __( 'New Product' ),
        'view_item'          => __( 'View Product' ),
        'search_items'       => __( 'Search Products' ),
        'not_found'          => __( 'No Products Found' ),
        'not_found_in_trash' => __( 'No products Found In Trash' ),
        'parent_item_colon'  => '',
        'menu_name'          => __( 'Products' ),
    );
    $args   = array(
        'labels'             => $labels,
        'menu_icon'          => 'dashicons-admin-post',
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'capability_type'    => 'post',
        'hierarchical'       => true,
        'menu_position'      => null,
        'has_archive'        => false,
        'rewrite'            => array(
            'slug' => 'products/%category%',
        ),
        'query_var'         => true,
        'supports'           => array(
            'title',
            'editor',
            'thumbnail',
            'page-attributes',
            'comments',
            'author',
        ),
    );
    register_post_type( 'products', $args );
}

add_action( 'init', 'create_products_taxonomies', 0 );
// Creates taxonomy for product category.
function create_products_taxonomies() {
    $labels = array(
        'name'              => __( 'Product Categories', 'taxonomy general name' ),
        'singular_name'     => __( 'Product Category', 'taxonomy singular name' ),
        'search_items'      => __( 'Search Product Categories' ),
        'all_items'         => __( 'All Product Categories' ),
        'parent_item'       => __( 'Parent' ),
        'parent_item_colon' => __( 'Parent:' ),
        'edit_item'         => __( 'Edit Product Category' ),
        'update_item'       => __( 'Update Product Category' ),
        'add_new_item'      => __( 'Add New Product Category' ),
        'new_item_name'     => __( 'New Product Category' ),
        'menu_name'         => __( 'Product Categories' ),
    );
    $args   = array(
        'labels'            => $labels,
        'hierarchical'      => true,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite' => array(
                'slug' => 'products',
                'hierarchical'      => true,
        ),
    );
    register_taxonomy( 'product_category', array( 'products' ), $args );
}

add_filter( 'post_type_link', 'cake_product_permalink_structure', 10, 4 );
function cake_product_permalink_structure( $post_link, $post, $leavename, $sample ) {
    if ( false !== strpos( $post_link, '%category%' ) ) {
        $product_type_term = get_the_terms( $post->ID, 'product_category' );
        if ( empty($product_type_term[0]->parent) ) {
            $post_link = str_replace( '%category%', array_pop( $product_type_term )->slug, $post_link );
        }
    }

    return $post_link;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.