WordPress文章自动添加自定义分类

作者:stoat 发布时间:2015-01-22 分类:wordpress

wordpress非常自由,可自定义任何文章类型,配合上自定义参数,制作定制类网站非常方便,无需修改数据库。

相关函数


注册自定义文章类型:
register_post_type
自定义文章分类:
register_taxonomy

WordPress文章自定义文章类型及分类代码参考


add_action( 'init', 'create_products', 0 );
function create_products() {
	$labels = array(
		'name' => '所有产品',
		'singular_name' => '产品',
		'menu_name' => '所有产品',
		'add_new' => '新增产品',
		'add_new_item' => '添加产品',
		'edit' => '编辑',
		'edit_item' => '编辑产品',
		'new_item' => '新产品',
		'view' => '查看产品',
		'view_item' => ' 查看产品',
		'search_items' => '搜索产品',
		'not_found' => '未找到产品',
		'not_found_in_trash' => '未在回收站中找到产品文章',
		'parent' => '父级产品'
	);
	$args = array(
		'labels' => $labels,
		'description' => '',
		'public' => true,
		'show_ui' => true,
		'show_in_menu' => true,
		'capability_type' => 'post',
		'hierarchical' => false,
		'taxonomies' => array('post_tag'),//此处可添加标签和分类'taxonomies' => array('category','post_tag')
		'menu_position' => 5,
		'rewrite' => array('slug' => 'products'),
		'query_var' => true,
		//'menu_icon' => get_stylesheet_directory_uri() . '/images/xx.png',
		'supports' => array('title','editor','thumbnail', 'custom-fields'),
		'publicly_queryable' => true,
		'has_archive' => true
	);
	register_post_type('products',$args);
	register_taxonomy("caty", array("products"), array("hierarchical" =>  true, "label" => "产品分类", "singular_label" => "产品分类", "rewrite" => true));
}

关于register_taxonomy的完整案例


function create_products_caty(){
	$labels = array(
        'name'                       => 'Product Category',
        'singular_name'              => 'Product Categories',
        'menu_name'                  => 'Product Category',
        'all_items'                  => 'All Product Categories',
        'parent_item'                => 'Parent Product Category',
        'parent_item_colon'          => 'Parent Product Category:',
        'new_item_name'              => 'New Product Category Name',
        'add_new_item'               => 'Add New Product Category',
        'edit_item'                  => 'Edit Product Category',
        'update_item'                => 'Update Product Category',
        'separate_items_with_commas' => 'Separate product categories with commas',
        'search_items'               => 'Search product categories',
        'add_or_remove_items'        => 'Add or remove product categories',
        'choose_from_most_used'      => 'Choose from the most used product categories',
    );
    $rewrite = array(
        'slug'                       => 'products',
        'with_front'                 => true,
        'hierarchical'               => true,
    );
    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => false,
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => true,
        'query_var'                  => 'products',
        'rewrite'                    => $rewrite,
    );
    register_taxonomy( 'products', 'product', $args );
}

相关参考


WordPress文章发布自动添加自定义栏目

原文地址:WordPress文章自动添加自定义分类 by 雪鼬博客

标签:wordpress

评论已关闭