使用 CPT slug 修改自定义分类 slug
Posted
技术标签:
【中文标题】使用 CPT slug 修改自定义分类 slug【英文标题】:Modify Custom Taxonomy slug with CPT slug 【发布时间】:2020-11-04 03:44:42 【问题描述】:您好,请在下面查看我的要求。
首先,我注册了一个CPT“目录”,其名称为“目录”。另外,我已经注册了一个自定义分类法“business_category”。我想在永久链接结构下面。
-
CPT 存档链接:www.domain.com/directory/
分类链接:www.domain.com/directory/category/TAXONOMY_NAME/
彩管单页:www.domain.com/directory/POST_NAME
所以,我使用了下面的代码。
public function __construct( $plugin_name, $version )
$this->plugin_name = $plugin_name;
$this->version = $version;
$this->PT = 'cc-directory';
$this->name = 'Directory';
$this->singular_name = 'Directory';
$this->slug = 'directory';
public function register_post_type()
// Get supported features for Directory post type
$supports = apply_filters('cc_directory_supports', array('editor', 'title','thumbnail'));
$labels = array(
'name' => $this->name,
'singular_name' => $this->singular_name,
'add_new' => 'Add New',
'add_new_item' => 'Add New ' . $this->singular_name,
'edit_item' => 'Edit ' . $this->singular_name,
'new_item' => 'New ' . $this->singular_name,
'all_items' => 'All ' . $this->name,
'view_item' => 'View ' . $this->name,
'search_items' => 'Search ' . $this->name,
'not_found' => 'No ' . strtolower($this->name) . ' found',
'not_found_in_trash' => 'No ' . strtolower($this->name) . ' found in Trash',
'parent_item_colon' => '',
'menu_name' => $this->name
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => $this->slug ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => true,
'menu_position' => 11,
'menu_icon' => 'dashicons-book',
'supports' => $supports,
'yarpp_support' => true
);
register_post_type( $this->PT, $args );
$plural_label = 'Categories';
$singular_label = 'Category';
register_taxonomy(
'business_category',
$this->PT,
array(
'label' => $plural_label,
'labels' => array(
'name' => $plural_label,
'singular_name' => $singular_label,
'all_items' => sprintf(__('All %s', 'claritycloud-directory'), $plural_label),
'edit_item' => sprintf(__('Edit %s', 'claritycloud-directory'), $singular_label),
'view_item' => sprintf(__('View %s', 'claritycloud-directory'), $singular_label),
'update_item' => sprintf(__('Update %s', 'claritycloud-directory'), $singular_label),
'add_new_item' => sprintf(__('Add New %s', 'claritycloud-directory'), $singular_label),
'new_item_name' => sprintf(__('New %s Name', 'claritycloud-directory'), $singular_label),
'popular_items' => sprintf(__('Popular %s', 'claritycloud-directory'), $plural_label),
'search_items' => sprintf(__('Search %s', 'claritycloud-directory'), $plural_label),
),
'public' => true,
'show_ui' => true,
'show_in_rest' => true,
'hierarchical' => true,
'rewrite' => array('slug' => 'business-category'),
)
);
现在我的网址是:
-
http://localhost/demo-project/directory/
http://localhost/demo-project/business-category/antiques/
http://localhost/demo-project/directory/a-lil-bit-of-sas/
如下所示:
-
http://localhost/demo-project/directory/
http://localhost/demo-project/directory/category/antiques/
http://localhost/demo-project/directory/a-lil-bit-of-sas/
只需要修改上面的第二个网址。
谁能给我建议?
谢谢, 苏汉卡
【问题讨论】:
请告诉我。 【参考方案1】:最后,我在这里得到了解决方案:
add_action('init', 'cpt_resources');
函数 cpt_resources()
$labels = array(
'name' => _x('Resources', 'snt'),
'singular_name' => _x('Resource', 'snt'),
'add_new' => _x('Add Resource', 'snt'),
'add_new_item' => __('Add Resource'),
'edit_item' => __('Edit Resource'),
'new_item' => __('New Resource'),
'view_item' => __('View Resource'),
'search_items' => __('Search Resources'),
'not_found' => __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'taxonomies' => array('resource_type'),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'has_archive' => true,
'rewrite' => array( 'slug' => 'resources' ),
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title','thumbnail', 'editor' ),
);
register_post_type( 'resources_post_type' , $args );
函数资源类型()
$labels = array(
'name' => _x( 'Resource Types', 'Taxonomy General Name', 'snt' ),
'singular_name' => _x( 'Resource Type', 'Taxonomy Singular Name', 'snt' ),
'menu_name' => __( 'Resource Types', 'snt' ),
'all_items' => __( 'All Items', 'snt' ),
'parent_item' => __( 'Parent Item', 'snt' ),
'parent_item_colon' => __( 'Parent Item:', 'snt' ),
'new_item_name' => __( 'New Item Name', 'snt' ),
'add_new_item' => __( 'Add New Item', 'snt' ),
'edit_item' => __( 'Edit Item', 'snt' ),
'update_item' => __( 'Update Item', 'snt' ),
'view_item' => __( 'View Item', 'snt' ),
'separate_items_with_commas' => __( 'Separate items with commas', 'snt' ),
'add_or_remove_items' => __( 'Add or remove items', 'snt' ),
'choose_from_most_used' => __( 'Choose from the most used', 'snt' ),
'popular_items' => __( 'Popular Items', 'snt' ),
'search_items' => __( 'Search Items', 'snt' ),
'not_found' => __( 'Not Found', 'snt' ),
'no_terms' => __( 'No items', 'snt' ),
'items_list' => __( 'Items list', 'snt' ),
'items_list_navigation' => __( 'Items list navigation', 'snt' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'rewrite' => array('slug' => 'resources/category')
);
register_taxonomy( 'resource_type', array( 'resources_post_type' ), $args );
add_action('init', 'resource_type', 0);
函数资源_cpt_generating_rule($wp_rewrite)
$rules = array();
$terms = get_terms( array(
'taxonomy' => 'resource_type',
'hide_empty' => false,
) );
$post_type = 'resources_post_type';
foreach ($terms as $term)
$rules['resources/category/' . $term->slug . '/([^/]*)$'] = 'index.php?post_type=' . $post_type. '&resources_post_type=$matches[1]&name=$matches[1]';
// merge with global rules
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
add_filter('generate_rewrite_rules', 'resources_cpt_generating_rule');
结果: http://localhost/demo-wp/resources/
http://localhost/demo-wp/resources/category/test-resources/
http://localhost/demo-wp/resources/final-check/
【讨论】:
以上是关于使用 CPT slug 修改自定义分类 slug的主要内容,如果未能解决你的问题,请参考以下文章
Wordpress // 删除 URL 的帖子类型 slug