创建自定义分类

时间:2013-10-08 17:09:09

标签: taxonomy

我在创建自定义分类时面临问题。

问题是,我正在使用zenshop wordpress主题。可以选择添加产品,但是当您发布产品时,已发布产品的URL就像这样

example.com/products/my-post-title 我想要它 example.com/mobiles/my-post-title

类似地,

当我在其中创建一个类别时,

example.com/product-category/name-of-category 我想要它 example.com/listing/name-of-category

Product.php代码如下

enter code here<?php    
add_action('init', 'project_products_init');  

/*-- Custom Post Init Begin --*/
function project_products_init()
{
  $labels = array(
    'name' => _x('Products', 'post type general name'),
    'singular_name' => _x('Product', 'post type singular name'),
    'add_new' => _x('Add New', 'product'),
    'add_new_item' => __('Add New Product'),
    'edit_item' => __('Edit Product'),
    'new_item' => __('New Product'),
    'view_item' => __('View Product'),
    'search_items' => __('Search Products'),
    'not_found' =>  __('No products found'),
    'not_found_in_trash' => __('No products found in Trash'),
    'parent_item_colon' => '',
    'menu_name' => 'Products'

  );

 $args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'query_var' => true,
    'rewrite' => true,
    'capability_type' => 'post',
    'has_archive' => true,
    'hierarchical' => false,
    'menu_position' => null,
    'supports' => array('title','editor','author','thumbnail','excerpt','custom-fields','comments')
  );
  // The following is the main step where we register the post.
  register_post_type('products',$args);




  // Initialize New Taxonomy Labels
  $labels = array(
    'name' => _x( 'Category', 'taxonomy general name' ),
    'singular_name' => _x( 'Category', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Category' ),
    'all_items' => __( 'All Category' ),
    'parent_item' => __( 'Parent Category' ),
    'parent_item_colon' => __( 'Parent Category:' ),
    'edit_item' => __( 'Edit Category' ),
    'update_item' => __( 'Update Category' ),
    'add_new_item' => __( 'Add New Category' ),
    'new_item_name' => __( 'New Category Name' ),
  );

// Custom taxonomy for Project Tags
register_taxonomy('product-category',array('products'), array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'product-category' ),
  ));

}
/*-- Custom Post Init Ends --*/

&GT;

请帮帮我。提前谢谢。

1 个答案:

答案 0 :(得分:0)

  

自定义分类代码

add_action( 'init', 'add_my_taxonomies', 0 );
function add_years_taxonomies() {
register_taxonomy('years', 'repository', array(
'hierarchical' => true,'labels' => array('labels' => array(
  'name' => _x( 'Years', 'taxonomy general name' ),
  'singular_name' => _x( 'Year', 'taxonomy singular name' ),
  'search_items' =>  __( 'Search Years' ),
  'all_items' => __( 'All Years' ),
  'parent_item' => __( 'Parent ' ),
  'parent_item_colon' => __( 'Parent Year:' ),
  'edit_item' => __( 'Edit Year' ),
  'update_item' => __( 'Update Year' ),
  'add_new_item' => __( 'Add New Year' ),
  'new_item_name' => __( 'New Year Name' ),
  'menu_name' => __( 'Years' ),
),
'rewrite' => array(
  'slug' => 'years', 
  'with_front' => false, 
  'hierarchical' => true 
),
));
}