关注VPS主机与
服务器促销分享

如何在WordPress主题添加自定义文章类型和分类的方法教程

WordPress是世界上使用最广泛的博客系统之一,是一款开源的PHP软件。因为使用者众多,所以WordPress社区非常活跃,有丰富的插件模板资源,使用WordPress可以快速搭建独立的博客网站。

作为一款简单实用的CMS系统,WordPress因为功能强大,便于开发,一直得到很多站长的青睐。最实用之处就是支持自定义文章类型和分类,并且非常的好用。本文章就来给大家简单讲解一下如何在WordPress主题中添加自定义文章类型register_post_type和分类register_taxonomy的方法教程。

1、添加自定义文章类型

/* Register Custom Post Type */
add_action( 'init', 'create_products_post_type' );
// add portfolio
function create_products_post_type() {
	$labels = array(
		'name'               => __('产品', 'WPGP'),
		'singular_name'      => __('产品', 'WPGP'),
		'add_new'            => __('添加', 'WPGP'),
		'add_new_item'       => __('新增产品', 'WPGP'),
		'edit_item'          => __('编辑产品', 'WPGP'),
		'new-item'           => __('新增产品', 'WPGP'),
		'view_item'          => __('查看产品', 'WPGP'),
		'search_items'       => __('搜索产品', 'WPGP'),
		'not_found'          => __('未找到产品', 'WPGP'),
		'not_found_in_trash' => __('垃圾箱未找到产品', 'WPGP'),
		'parent_item_colon'  => '',
	);

	$args = array(
		'labels'             => $labels,
		'show_ui'            => true,  // Whether to generate a default UI for managing this post type in the admin
		'query_var'          => true,
		'show_in_nav_menus'  => false,
		'public'             => true,  // Controls how the type is visible to authors and readers
		'capability_type'    => 'post',
		'hierarchical'       => false,
		'menu_icon'          => 'dashicons-format-gallery', // use a font icon, e.g. 'dashicons-chart-pie'
		'has_archive'        => true,  // Enables post type archives
		'rewrite'            => array( 'slug' => 'products' ),
		'supports'           => array( 'title', 'editor',  'thumbnail', 'excerpt', 'comments', 'custom-fields', 'page-attributes' ),
		'can_export'         => true,
	);

	register_post_type( 'products', $args );
}

2、添加分类功能

add_action( 'init', 'register_products_taxonomy');

// create two taxonomies, genres and writers for the post type "book"
function register_products_taxonomy() {
	// Add new taxonomy, make it hierarchical (like categories)
	$labels = array(
		'name'              => __('产品分类', 'WPGP'),
		'singular_name'     => __('产品分类', 'WPGP'),
		'menu_name'         => __('产品分类', 'WPGP'),
		'search_items'      => __('搜索', 'WPGP'),
		'all_items'         => __('所有产品分类', 'WPGP'),
		'parent_item'       => __( '该产品分类的上级分类' ),
		'parent_item_colon' => __( '该产品分类的上级分类:' ),
		'edit_item'         => __('编辑产品分类', 'WPGP'),
		'update_item'       => __('更新产品分类', 'WPGP'),
		'add_new_item'      => __('添加新的产品分类', 'WPGP'),
		'new_item_name'     => __('新的产品分类', 'WPGP'),
	);

	$args = array(
		'hierarchical'      => true,
		'labels'            => $labels,
		'show_ui'           => true,
		'show_in_menu'      => true,
		'show_in_nav_menus' => true,   
		'query_var'         => true,
		'has_archive'       => false,
                'show_admin_column' => true,
		'rewrite'           => array( 'slug' => 'product' ),
	);

	register_taxonomy( 'product', 'products', $args );
}

3、添加后台自定义文章排序的功能

// admin page products orderby
add_filter( 'parse_query', 'sort_products_by_date' );
function sort_products_by_date() {
	global $pagenow;

	if ( is_admin() && $pagenow =='edit.php' &&  !empty($_GET['post_type'] == 'products') && !isset($_GET['post_status']) && !isset($_GET['orderby']) ) {
		wp_redirect( admin_url('edit.php?post_type=products&orderby=date&order=desc') );
		exit;
	}
}

WordPress官方支持中文版,同时有爱好者开发的第三方中文语言包,如wopus中文语言包。WordPress拥有成千上万个各式插件和不计其数的主题模板样式。

赞(0)
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权, 转载请注明出处。
文章名称:《如何在WordPress主题添加自定义文章类型和分类的方法教程》
文章链接:https://www.zyhot.com/article/12782.html
关于安全:任何IDC都有倒闭和跑路的可能,月付和备份是您的最佳选择,请保持良好的、有规则的备份习惯。
本站声明:本站仅做信息分享,不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本站请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本站,即表示您已经知晓并接受了此声明通告。

评论 抢沙发

评论前必须登录!

 

登录

找回密码

注册