url 中的 Wordpress CPT 多个类别
Wordpress CPT multiple categories in the url
我有一个 CPT Traffi 办公室。
Url: www.domain.com/traffic-offices/
当添加一个新的喜欢时 www.domain.com/traffic-offices/germany/
就可以了。
当我添加一个新的 post Saksen 时,它会自动生成 url www.domain.com/traffic-offices/saksen
,但我想要 www.domain.com/traffic-offices/germany/saksen
。如果我填写它会自动使 www.domain.com/traffic-offices/germany-saksen
.
我怎样才能做到这一点 url www.domain.com/traffic-offices/germany/saksen
?
function trafficoffice_post_type() {
// Labels
$labels = array(
'name' => _x("trafficoffices", "post type general name"),
'singular_name' => _x("trafficoffices", "post type singular name"),
'menu_name' => 'trafficoffices',
'add_new' => _x("Add new", "trafficoffice item"),
'add_new_item' => __("Add new"),
'edit_item' => __("trafficoffice aanpassen"),
'new_item' => __("Add new trafficoffice"),
'view_item' => __("View trafficoffice"),
'search_items' => __("Search Profiles"),
'not_found' => __("No Profiles Found"),
'not_found_in_trash' => __("No Profiles Found in Trash"),
'parent_item_colon' => ''
);
// Register post type
register_post_type('trafficoffice', array(
'labels' => $labels,
'public' => true,
'has_archive' => false,
'rewrite' => array(
'slug' => 'traffic-offices',
),
'supports' => array('title', 'editor', 'thumbnail')
));
}
add_action('init', 'trafficoffice_post_type', 0);
一种方法是使用分类法。
- 注册一个名为 "country" 的新分类法。
- 使用 slug "germany" 创建一个新类别 "Germany"。
- 将类别 "Germany" 分配给 post "Saksen"。
这是注册分类法的代码:
$labels = array(
'name' => _x( 'Countries', 'taxonomy general name', 'textdomain' ),
'singular_name' => _x( 'Country', 'taxonomy singular name', 'textdomain' ),
'search_items' => __( 'Search Countries', 'textdomain' ),
'popular_items' => __( 'Popular Countries', 'textdomain' ),
'all_items' => __( 'All Countries', 'textdomain' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Country', 'textdomain' ),
'update_item' => __( 'Update Country', 'textdomain' ),
'add_new_item' => __( 'Add New Country', 'textdomain' ),
'new_item_name' => __( 'New Country Name', 'textdomain' ),
'separate_items_with_commas' => __( 'Separate Countries with commas', 'textdomain' ),
'add_or_remove_items' => __( 'Add or remove Countries', 'textdomain' ),
'choose_from_most_used' => __( 'Choose from the most used Countries', 'textdomain' ),
'not_found' => __( 'No Countries found.', 'textdomain' ),
'menu_name' => __( 'Countries', 'textdomain' ),
);
$args = array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
//'rewrite' => array( 'slug' => 'country' ),
);
register_taxonomy( 'country', 'trafficoffice', $args );
确保 WordPress 的永久链接设置(设置 -> 永久链接)包括类别。
/%category%/%postname%/
详细了解 register_taxonomy()。
我有一个 CPT Traffi 办公室。 Url: www.domain.com/traffic-offices/
当添加一个新的喜欢时 www.domain.com/traffic-offices/germany/
就可以了。
当我添加一个新的 post Saksen 时,它会自动生成 url www.domain.com/traffic-offices/saksen
,但我想要 www.domain.com/traffic-offices/germany/saksen
。如果我填写它会自动使 www.domain.com/traffic-offices/germany-saksen
.
我怎样才能做到这一点 url www.domain.com/traffic-offices/germany/saksen
?
function trafficoffice_post_type() {
// Labels
$labels = array(
'name' => _x("trafficoffices", "post type general name"),
'singular_name' => _x("trafficoffices", "post type singular name"),
'menu_name' => 'trafficoffices',
'add_new' => _x("Add new", "trafficoffice item"),
'add_new_item' => __("Add new"),
'edit_item' => __("trafficoffice aanpassen"),
'new_item' => __("Add new trafficoffice"),
'view_item' => __("View trafficoffice"),
'search_items' => __("Search Profiles"),
'not_found' => __("No Profiles Found"),
'not_found_in_trash' => __("No Profiles Found in Trash"),
'parent_item_colon' => ''
);
// Register post type
register_post_type('trafficoffice', array(
'labels' => $labels,
'public' => true,
'has_archive' => false,
'rewrite' => array(
'slug' => 'traffic-offices',
),
'supports' => array('title', 'editor', 'thumbnail')
));
}
add_action('init', 'trafficoffice_post_type', 0);
一种方法是使用分类法。
- 注册一个名为 "country" 的新分类法。
- 使用 slug "germany" 创建一个新类别 "Germany"。
- 将类别 "Germany" 分配给 post "Saksen"。
这是注册分类法的代码:
$labels = array(
'name' => _x( 'Countries', 'taxonomy general name', 'textdomain' ),
'singular_name' => _x( 'Country', 'taxonomy singular name', 'textdomain' ),
'search_items' => __( 'Search Countries', 'textdomain' ),
'popular_items' => __( 'Popular Countries', 'textdomain' ),
'all_items' => __( 'All Countries', 'textdomain' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Country', 'textdomain' ),
'update_item' => __( 'Update Country', 'textdomain' ),
'add_new_item' => __( 'Add New Country', 'textdomain' ),
'new_item_name' => __( 'New Country Name', 'textdomain' ),
'separate_items_with_commas' => __( 'Separate Countries with commas', 'textdomain' ),
'add_or_remove_items' => __( 'Add or remove Countries', 'textdomain' ),
'choose_from_most_used' => __( 'Choose from the most used Countries', 'textdomain' ),
'not_found' => __( 'No Countries found.', 'textdomain' ),
'menu_name' => __( 'Countries', 'textdomain' ),
);
$args = array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
//'rewrite' => array( 'slug' => 'country' ),
);
register_taxonomy( 'country', 'trafficoffice', $args );
确保 WordPress 的永久链接设置(设置 -> 永久链接)包括类别。
/%category%/%postname%/
详细了解 register_taxonomy()。