wordpress进阶教程(三):创建自定义分类法

前面刚建了给wordpress创建新的文章类型函数:register_post_type()该函数还有个taxonomies参数,用来给自定义文章类型添加分类法制止,给wordpress添加默认的分类和标签支持方法很简单,只需要在创建文章类型的时候设置taxonomies参数如下:

  1. 'taxonomies'=> array('category','post_tag'),  

效果如图:

自定义文章类型添加分类支持

然后这在实际中的用法很少,一般情况下我们使用新的分类法,比如我们要做一个企业站,给产品做了一个新的文章类型product。我们要给产品归类,可以按类别分,比如电脑、手机。还可以按产地分,比如:国产山寨、水货,还可以按价格区间分,比如:1000元以下、1000到3000,等等,我们就需要同时有3个分类法:类别、产地、价格区间。

本部分教程的第一篇中我们也提到了,在wp-includes/post.php文件中wordpress使用register_taxonomy函数创建了分类-category和标签-post_tag两个分类法。

下面是register_taxonomy函数的参数好用法:

  1. <?php   
  2. register_taxonomy($taxonomy$object_type$args);   
  3. //$taxonomy 字符串型,必需,分类法的名称,用英文哦   
  4. //$object_type 数组或字符串,必需,分类法所对应的文章类型   
  5. //$args--可选,配置参数   
  6. ?>   

$args参数是个数组,跟register_post_type函数的$args参数类似,详细:
label-
labels-数组:

  • 'name'
  • 'singular_name'
  • 'search_items'
  • 'popular_items'
  • 'all_items'
  • 'parent_item'
  • ....略

public-
show_in_nav_menus-是否在菜单设置页面显示
....略,参考register_post_type函数

实例,在前面的一篇文章中,我们新建了一个book文章类型,下面我们为这个book文章类型添加一个国家-country分类法支持,完整代码如下(保留了上面添加的分类和标签支持):

  1. <?php   
  2. add_action('init', 'my_custom_init');   
  3. function my_custom_init()    
  4. {   
  5.   $labels = array(   
  6.     'name' => '书本name',   
  7.     'singular_name' => '书本singularname',   
  8.     'add_new' => 'Add_new',   
  9.     'add_new_item' => 'add_new_item',   
  10.     'edit_item' => 'edit_item',   
  11.     'new_item' => 'new_item',   
  12.     'view_item' => 'view_item',   
  13.     'search_items' => 'search_items',   
  14.     'not_found' =>  'not_found',   
  15.     'not_found_in_trash' => 'not_found_in_trash',    
  16.     'parent_item_colon' => '',   
  17.     'menu_name' => 'menu_name'   
  18.   
  19.   );   
  20.   $args = array(   
  21.     'labels' => $labels,   
  22.     'description'=> '嘿,这是一个自定义的文章类型',   
  23.     'public' => true,
  24.     'publicly_queryable' => true,   
  25.     'show_ui' => true,    
  26.     'show_in_menu' => true,    
  27.     'query_var' => true,   
  28.     'rewrite' => true,   
  29.     'capability_type' => 'post',   
  30.     'has_archive' => true,    
  31.     'hierarchical' => false,   
  32.     'menu_position' => null,   
  33.     'taxonomies'=> array('category','post_tag'),   
  34.     'supports' => array('title','editor','author','thumbnail','excerpt','comments')   
  35.   );    
  36.   register_post_type('book',$args);   
  37.      
  38.   $labels = array(   
  39.         'name' => '国籍',    
  40.         'singular_name' => 'country',   
  41.         'search_items' =>  '搜索' ,   
  42.         'popular_items' => '热门' ,   
  43.         'all_items' => '所有' ,   
  44.         'parent_item' => null,   
  45.         'parent_item_colon' => null,   
  46.         'edit_item' => '编辑' ,    
  47.         'update_item' => '更新' ,   
  48.         'add_new_item' => '添加' ,   
  49.         'new_item_name' => '国籍名称',   
  50.         'separate_items_with_commas' => '按逗号分开' ,   
  51.         'add_or_remove_items' => '添加或删除',   
  52.         'choose_from_most_used' => '从经常使用的类型中选择',   
  53.         'menu_name' => '国籍分类',   
  54.     );    
  55.   
  56.     register_taxonomy(   
  57.         'country',   
  58.         array('book'),   
  59.         array(   
  60.             'hierarchical' => true,   
  61.             'labels' => $labels,   
  62.             'show_ui' => true,   
  63.             'query_var' => true,   
  64.             'rewrite' => array( 'slug' => 'country' ),   
  65.         )   
  66.     );   
  67. }   
  68. ?>  

添加国籍分类法

 

更新--前台使用方法

文章类型归档模板:如果你需要一个现实所有该文章类型的模板,请在后台新建一个archive-{post_type}.php,比如上面的book类型,新建archive-book.php,用这个模板文件默认可显示所有book类型的文章。

分类模板:taxonomy-{taxonomy_slug}.php-这是自定义分类法的分类页,比如上面代码中我们新建了一个分类法country,使用taxonomy-country.php文件,就是这个分类法的分类页面了。

代码获取分类:对于默认的分类,我们可以使用get_categories()函数来获取分类,对于自定义分类法,这个函数同样适用,只是注意taxonomy 参数。

通过ID获取分类连接:默认分类我们通过分类ID获取分类链接是使用函数:get_category_link()。但是自定义分类法我们应该使用get_term_link()函数,函数用法这里就不说了,请看官自己到官网查看。

已有75条评论

Jason进行回复 取消回复