I have created a custom post type portfolio with custom taxonomy Portfolio Category. I can create portfolio items and assign portfolio categories to them and save these custom posts. But what I need is the permalink to show domain.com/portfolio-category/portfolio-item-name
. Portfolio Category is variable depending on the category I pick.
Now I only see domain.com/portfolio/item-name
The name portfolio should be the name of the custom taxonomy I attach the custom post to, but it does not. Is just a fixed name from the CPT it seems.
I would like urls like domain.com/design/client-a
and domain.com/dev/client-b
Here is my code:
// Register Custom Taxonomy
function portfolio_taxonomy() {
$labels = array(
'name' => _x( 'Portfolio Category', 'Taxonomy General Name', 'imagewize_portfolio_plugin' ),
'singular_name' => _x( 'Portfolio Category', 'Taxonomy Singular Name', 'imagewize_portfolio_plugin' ),
'menu_name' => __( 'Category', 'imagewize_portfolio_plugin' ),
'all_items' => __( 'All Items', 'imagewize_portfolio_plugin' ),
'parent_item' => __( 'Parent Item', 'imagewize_portfolio_plugin' ),
'parent_item_colon' => __( 'Parent Item:', 'imagewize_portfolio_plugin' ),
'new_item_name' => __( 'New Item Name', 'imagewize_portfolio_plugin' ),
'add_new_item' => __( 'Add New Item', 'imagewize_portfolio_plugin' ),
'edit_item' => __( 'Edit Item', 'imagewize_portfolio_plugin' ),
'update_item' => __( 'Update Item', 'imagewize_portfolio_plugin' ),
'separate_items_with_commas' => __( 'Separate items with commas', 'imagewize_portfolio_plugin' ),
'search_items' => __( 'Search Items', 'imagewize_portfolio_plugin' ),
'add_or_remove_items' => __( 'Add or remove items', 'imagewize_portfolio_plugin' ),
'choose_from_most_used' => __( 'Choose from the most used items', 'imagewize_portfolio_plugin' ),
'not_found' => __( 'Not Found', 'imagewize_portfolio_plugin' ),
);
$rewrite = array(
'slug' => 'Portfolio Category',
'with_front' => true,
'hierarchical' => true,
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'rewrite' => $rewrite,
);
register_taxonomy( 'portfolio', array( 'Img_portfolio_cpt' ), $args );
}
// Hook into the 'init' action
add_action( 'init', 'portfolio_taxonomy', 0 );
if ( ! function_exists('img_portfolio_cpt') ) {
// Register Custom Post Type
function img_portfolio_cpt() {
$labels = array(
'name' => _x( 'Portfolio Items', 'Post Type General Name', 'imagewize_portfolio_plugin' ),
'singular_name' => _x( 'Portfolio', 'Post Type Singular Name', 'imagewize_portfolio_plugin' ),
'menu_name' => __( 'Portfolio', 'imagewize_portfolio_plugin' ),
'parent_item_colon' => __( 'Parent Item:', 'imagewize_portfolio_plugin' ),
'all_items' => __( 'All Items', 'imagewize_portfolio_plugin' ),
'view_item' => __( 'View Item', 'imagewize_portfolio_plugin' ),
'add_new_item' => __( 'Add New Item', 'imagewize_portfolio_plugin' ),
'add_new' => __( 'Add New', 'imagewize_portfolio_plugin' ),
'edit_item' => __( 'Edit Item', 'imagewize_portfolio_plugin' ),
'update_item' => __( 'Update Item', 'imagewize_portfolio_plugin' ),
'search_items' => __( 'Search Item', 'imagewize_portfolio_plugin' ),
'not_found' => __( 'Not found', 'imagewize_portfolio_plugin' ),
'not_found_in_trash' => __( 'Not found in Trash', 'imagewize_portfolio_plugin' ),
);
$rewrite = array(
'slug' => 'portfolio',
'with_front' => true,
'pages' => true,
'feeds' => true,
);
$args = array(
'label' => __( 'Img_portfolio_cpt', 'imagewize_portfolio_plugin' ),
'description' => __( 'Imagewize Portfolio', 'imagewize_portfolio_plugin' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', ),
'taxonomies' => array( 'portfolio' ),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-portfolio',
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'rewrite' => $rewrite,
'capability_type' => 'page',
);
register_post_type( 'Img_portfolio_cpt', $args );
}
// Hook into the 'init' action
add_action( 'init', 'img_portfolio_cpt', 0 );
}
post_type_link
might be a solution.