2

I have the following custom post-type and custom taxonomy setup:

add_action( 'init', 'create_post_type' );
function create_post_type() {
    register_post_type( 'system',
        array(
            'labels' => array(
                'name' => __( 'Systems' ),
                'singular_name' => __( 'System' )
            ),
        'capability_type' => 'post',
        'supports' => array('title','editor','comments'),   
        'public' => true,
        'has_archive' => true,
        'rewrite' => array( 'slug' => 'system' ),
        )
    );
}

function news_init() {
    register_taxonomy(
        'system',
        'system',
        array(
            'label' => __( 'Product Category' ),
            'sort' => true,
            'hierarchical' => true,
            'args' => array( 'orderby' => 'term_order' ),
            'rewrite' => array( 'slug' => 'product-category' )
        )
    );  
}
add_action( 'init', 'news_init' );

Is it possible to include the custom taxonomy name in the URL?

At present when I goto a custom post the URL looks like this:

http://www.domain/product-category/(post-name)/

How can I make it look like the following?

http://www.domain/(category-slug)/(post-name)/

1

3 Answers 3

1

This can be a bit challenging for WordPress, because it wouldn't know whether you are referring to post, page or taxonomy. Nevertheless, rename your taxonomy to something like product_category and try changing the slug in your post type arguments to system/%product_category%, and slug in your taxonomy arguments to just system. WordPress should now handle /system/your-product-category/post-name/

Out of the box WordPress doesn't recognize the permalink structure tag %product_category%, so we need to declare it:

function filter_post_type_permalink($link, $post)
{
    if ($post->post_type != 'system')
        return $link;

    if ($cats = get_the_terms($post->ID, 'product_category'))
        $link = str_replace('%product_category%', array_pop($cats)->slug, $link);
    return $link;
}
add_filter('post_type_link', 'filter_post_type_permalink', 10, 2);

NOTES:

  1. This will just grab the first product category for the post ordered by name.
  2. There still may be some 404 errors, if pages, posts etc., have same names.
3
  • Thanks for the answer. No luck though. I'm still getting the 404 pages for all of the taxonomy pages. I even did the re-save permalinks to flush each time just in case but no luck.
    – Rob
    Commented Oct 24, 2012 at 14:16
  • Make sure use flush_rewrite_rules() to flush out everything. Add it to your hooks when you activate/deactivate your plugin ------------------------------------------ 'register_activation_hook( FILE, array( 'Your_Hook', 'on_activation' ) ); register_deactivation_hook( FILE, array( 'Your_Hook', 'on_deactivation' ) ); register_uninstall_hook( FILE, array( 'Your_Hook', 'on_uninstall' ) );'
    – user952851
    Commented Mar 7, 2014 at 13:29
  • I think this worked for me, but it doesn't actually adjust the permalink, it seems. If I go to the URL I want it to be (/product-category-slug/product-slug/) I get redirected to the correct product, but it's using it's normal permalink (/products/product-slug/). This may be good enough for my purposes, but I'd like to actually have it update the permalinks. Commented Jan 6, 2016 at 17:42
1

First off DO NOT USE SAME POST TYPE & TAXONOMY, In fact, you do not even need to register a taxonomy since you require category. Use the following code:

add_action( 'init', 'create_post_type' );
function create_post_type() {
    register_post_type( 'system',
        array(
            'labels' => array(
                'name' => __( 'Systems' ),
                'singular_name' => __( 'System' )
            ),
        'capability_type' => 'post',
        'supports' => array('title','editor','comments'),   
        'public' => true,
        'has_archive' => true,
        'rewrite' => array( 'slug' => '%category%' ),
        'taxonomies' => array('category')
        )
    );
}

Now you will have categories available for use with your 'system' post type. Also observe that I have changed the value of slug. Now next hook up with filter post_type_link , this is used to replace '%category%' with the slug of category. Use the following code:

function replace_system_type_category( $post_link, $id = 0 ){
    $post = get_post($id);  
    if ( is_object( $post ) ){
        $terms = wp_get_object_terms( $post->ID, 'category' );
        if( $terms ){
            return str_replace( '%category%' , $terms[0]->slug , $post_link );
        }
    }
    return $post_link;  
}
add_filter( 'post_type_link', 'replace_system_type_category', 1, 3 );

The above code with replace %category% with the slug of category as required by you. You can use the code with custom taxonomy however do not use 'system' for both post type and taxonomy.

0

This is what i tested locally and it displays the taxonomy type name you create in the settings page the code adds in the permalink on taxonomy archive pages.

add_action( 'init', 'wpsites_create_custom_taxonomy_types' );
function wpsites_create_custom_taxonomy_types() {

register_taxonomy( 'system-type', 'system',
    array(
        'labels' => array(
            'name'          => _x( 'Types', 'taxonomy general name', 'wpsites' ),
            'add_new_item'  => __( 'Add New System Type', 'wpsites' ),
            'new_item_name' => __( 'New System Type', 'wpsites' ),
        ),
        'exclude_from_search' => true,
        'has_archive'         => true,
        'hierarchical'        => true,
        'rewrite'             => array( 'slug' => 'system-type', 'with_front' => false ),
        'show_ui'             => true,
        'show_tagcloud'       => false,
    ));

}



add_action( 'init', 'wpsites_system_custom_post_type' );
function wpsites_system_custom_post_type() {

register_post_type( 'system',
    array(
        'labels' => array(
            'name'          => __( 'System', 'wpsites' ),
            'singular_name' => __( 'System', 'wpsites' ),
        ),
        'has_archive'  => true,
        'hierarchical' => true,
        'menu_icon'    => 'dashicons-admin-network',
        'public'       => true,
        'rewrite'      => array( 'slug' => 'system', 'with_front' => false ),
        'supports'     => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'revisions', 'page-attributes' ),
        'taxonomies'   => array( 'system-type' ),

    ));

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.