我应该仔细看看上面的链接。这个问题中有一个包含答案的链接(https://wordpress.stackexchange.com/a/5313/100538). 下面是我用来获得所需效果的代码:
///// CUSTOM POST TYPES /////
// register the new post type
register_post_type( \'business\', array(
\'labels\' => array(
\'name\' => __( \'Businesses\' ),
\'singular_name\' => __( \'Business\' ),
\'add_new\' => __( \'Add New\' ),
\'add_new_item\' => __( \'Create New Business\' ),
\'edit\' => __( \'Edit\' ),
\'edit_item\' => __( \'Edit Business\' ),
\'new_item\' => __( \'New Business\' ),
\'view\' => __( \'View Businesses\' ),
\'view_item\' => __( \'View Business\' ),
\'search_items\' => __( \'Search Businesses\' ),
\'not_found\' => __( \'No businesses found\' ),
\'not_found_in_trash\' => __( \'No businesses found in trash\' ),
\'parent\' => __( \'Parent Business\' ),
),
\'description\' => __( \'This is where you can create new businesses on your site.\' ),
\'public\' => true,
\'show_ui\' => true,
\'capability_type\' => \'post\',
\'publicly_queryable\' => true,
\'exclude_from_search\' => false,
\'has_archive\' => true,
\'menu_position\' => 2,
\'menu_icon\' => \'dashicons-id\',
\'hierarchical\' => true,
\'_builtin\' => false, // It\'s a custom post type, not built in!
\'rewrite\' => array( \'slug\' => \'business-listings/%business_cat%\', \'with_front\' => true ),
\'query_var\' => true,
\'supports\' => array( \'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'trackbacks\', \'custom-fields\', \'comments\', \'revisions\' ),
) );
//hook into the init action and call create_book_taxonomies when it fires
add_action( \'init\', \'create_product_taxonomies\', 0 );
//add_action(\'admin_init\', \'flush_rewrite_rules\');
//create two taxonomies, genres and writers for the post type "book"
function create_product_taxonomies() {
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
\'name\' => _x( \'Categories\', \'taxonomy general name\' ),
\'singular_name\' => _x( \'Category\', \'taxonomy singular name\' ),
\'search_items\' => __( \'Search Categories\' ),
\'all_items\' => __( \'All Categories\' ),
\'parent_item\' => __( \'Parent Categories\' ),
\'parent_item_colon\' => __( \'Parent Categories:\' ),
\'edit_item\' => __( \'Edit Category\' ),
\'update_item\' => __( \'Update Category\' ),
\'add_new_item\' => __( \'Add New Category\' ),
\'new_item_name\' => __( \'New Category Name\' ),
\'menu_name\' => __( \'Category\' ),
);
register_taxonomy( \'business_cat\', array( \'business\' ), array(
\'hierarchical\' => true,
\'labels\' => $labels,
\'show_ui\' => true,
\'query_var\' => true,
//\'rewrite\' => true,
\'rewrite\' => array( \'slug\' => \'business-listings\', \'with_front\' => true ),
) );
// Add new taxonomy, NOT hierarchical (like tags)
$labels = array(
\'name\' => _x( \'Scents\', \'taxonomy general name\' ),
\'singular_name\' => _x( \'Scent\', \'taxonomy singular name\' ),
\'search_items\' => __( \'Search Scents\' ),
\'popular_items\' => __( \'Popular Scents\' ),
\'all_items\' => __( \'All Scents\' ),
\'parent_item\' => null,
\'parent_item_colon\' => null,
\'edit_item\' => __( \'Edit Scent\' ),
\'update_item\' => __( \'Update Scent\' ),
\'add_new_item\' => __( \'Add New Scent\' ),
\'new_item_name\' => __( \'New Scent Name\' ),
\'separate_items_with_commas\' => __( \'Separate scents with commas\' ),
\'add_or_remove_items\' => __( \'Add or remove scents\' ),
\'choose_from_most_used\' => __( \'Choose from the most used scents\' ),
\'menu_name\' => __( \'Scents\' ),
);
register_taxonomy( \'scent\', \'business\', array(
\'hierarchical\' => false,
\'labels\' => $labels,
\'show_ui\' => true,
\'query_var\' => true,
//\'rewrite\' => array( \'slug\' => \'scents\' ),
) );
}
function wpse_5308_post_type_link( $link, $post ) {
if ( $post->post_type === \'business\' ) {
if ( $terms = get_the_terms( $post->ID, \'business_cat\' ) )
$link = str_replace( \'%business_cat%\', current( $terms )->slug, $link );
else
$link = str_replace( \'%business_cat%\', \'uncategorized\', $link );
}
return $link;
}
add_filter( \'post_type_link\', \'wpse_5308_post_type_link\', 10, 2 );
所有的功劳都归功于死亡医生的回答,我从中拼凑出了这个答案。