今天无意中,看到了wordpress的一篇有用文章,赶紧推荐给大家。
网上大部分人分享添加 WordPress 自定义类型文章的代码,一般都是包含了文章和分类,最多就是添加一个跟文章类型 post 共用一个标签库,但是默认标签库的数量是增加了,但是点击查看该标签文章却没有那么多,只能看到 post 类型的文章,而看不到自定义文章类型,所以最好的方案就是为自定义类型的文章弄一个独立的标签库。
将以下代码添加到当前主题的 functions.php 文件中,或者添加到一个文章类型的 post_type.php 文件,然后在 functions.php 文件中引用该文章类型文件即可。
-
// 商品
-
add_action( 'init', 'post_type_tao' );
-
function post_type_tao() {
-
$labels = array(
-
'name' => '商品', 'post type general name', 'your-plugin-textdomain',
-
'singular_name' => '商品', 'post type singular name', 'your-plugin-textdomain',
-
'menu_name' => '商品', 'admin menu', 'your-plugin-textdomain',
-
'name_admin_bar' => '商品', 'add new on admin bar', 'your-plugin-textdomain',
-
'add_new' => '发布商品', 'tao', 'your-plugin-textdomain',
-
'add_new_item' => '发布新商品', 'your-plugin-textdomain',
-
'new_item' => '新商品', 'your-plugin-textdomain',
-
'edit_item' => '编辑商品', 'your-plugin-textdomain',
-
'view_item' => '查看商品', 'your-plugin-textdomain',
-
'all_items' => '所有商品', 'your-plugin-textdomain',
-
'search_items' => '搜索商品', 'your-plugin-textdomain',
-
'parent_item_colon' => 'Parent 商品:', 'your-plugin-textdomain',
-
'not_found' => '你还没有发布商品。', 'your-plugin-textdomain',
-
'not_found_in_trash' => '回收站中没有商品。', 'your-plugin-textdomain'
-
);
-
$args = array(
-
'labels' => $labels,
-
'public' => true,
-
'publicly_queryable' => true,
-
'show_ui' => true,
-
'show_in_menu' => true,
-
'query_var' => true,
-
'rewrite' => array( 'slug' => tao ),
-
'capability_type' => 'post',
-
'menu_icon' => 'dashicons-cart',
-
'has_archive' => false,
-
'hierarchical' => false,
-
'menu_position' => 10,
-
'supports' => array( 'title', 'editor', 'author', 'excerpt', 'comments', 'thumbnail', 'revisions', 'custom-fields' )
-
);
-
register_post_type( 'tao', $args );
-
}
-
// 商品分类
-
add_action( 'init', 'create_tao_taxonomies', 0 );
-
function create_tao_taxonomies() {
-
$labels = array(
-
'name' => '商品分类目录', 'taxonomy general name',
-
'singular_name' => '商品分类', 'taxonomy singular name',
-
'search_items' => '搜索商品目录',
-
'all_items' => '所有商品目录',
-
'parent_item' => 'Parent Genre',
-
'parent_item_colon' => 'Parent Genre:',
-
'edit_item' => '编辑商品目录',
-
'update_item' => '更新商品目录',
-
'add_new_item' => '添加新商品目录',
-
'new_item_name' => 'New Genre Name',
-
'menu_name' => '商品分类',
-
);
-
$args = array(
-
'hierarchical' => true,
-
'labels' => $labels,
-
'show_ui' => true,
-
'show_admin_column' => true,
-
'query_var' => true,
-
'rewrite' => array( 'slug' => taobao),
-
);
-
register_taxonomy( 'taobao', array( 'tao' ), $args );
-
}
-
// 商品标签
-
add_action( 'init', 'create_tao_tag_taxonomies', 0 );
-
function create_tao_tag_taxonomies() {
-
$labels = array(
-
'name' => '商品标签', 'taxonomy general name',
-
'singular_name' => '商品标签', 'taxonomy singular name',
-
'search_items' => '搜索商品标签',
-
'all_items' => '所有商品标签',
-
'parent_item' => 'Parent Genre',
-
'parent_item_colon' => 'Parent Genre:',
-
'edit_item' => '编辑商品标签',
-
'update_item' => '更新商品标签',
-
'add_new_item' => '添加新商品标签',
-
'new_item_name' => 'New Genre Name',
-
'menu_name' => '商品标签',
-
);
-
$args = array(
-
'hierarchical' => false,
-
'labels' => $labels,
-
'show_ui' => true,
-
'show_admin_column' => true,
-
'query_var' => true,
-
'rewrite' => array( 'slug' => taotag ),
-
);
-
register_taxonomy( 'taotag', array( 'tao' ), $args );
-
}
以上代码实现的功能就是为 WordPress 站点添加自定义类型的文章、分类和标签,具体请自行修改里面自定义类型的标题、slug 等内容。想创建多个自定义类型文章、分类和标签,直接重复上述代码,并修改为其他的标题及 slug 即可。
- 我的微信
- 这是我的微信扫一扫
- 我的微信公众号
- 我的微信公众号扫一扫