wordpress定义新的POST类型

本文介绍如何在WordPress中注册一种新的Post类型——文档,并提供了一段可在functions.php文件中使用的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们知道wordpress中,文章,媒体,页面都是存放在wp_posts表中的。它们都被定义为一种post类型。wordpress提供了钩子让我们自己注册新的POST类型。下面我们注册一种新的类型“文档”。


/**
 * an example of registering a post type called "document" including providing contextual help
 */
add_action('init', 'codex_custom_init');
function codex_custom_init() {
	$labels = array (
		'name' => _x('文档',
		'post type general name'
	), 'singular_name' => _x('文档', 'post type singular name'), 'add_new' => _x('添加', 'document'), 'add_new_item' => __('添加文档'), 'edit_item' => __('编辑文档'), 'new_item' => __('新建'), 'all_items' => __('所有文档'), 'view_item' => __('查看文档'), 'search_items' => __('搜索文档'), 'not_found' => __('没有找到文档'), 'not_found_in_trash' => __('回收站中没有找到文档'), 'parent_item_colon' => '', 'menu_name' => '文档');
	$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',
			'comments'
		),
		'taxonomies' => array (
			'category',
			'post_tag'
			) // this is IMPORTANT


	);
	register_post_type('document', $args);
}


将上面的代码添加到functions.php中,然后在后台就会看到我们新注册的文档类型。
上面的代码调用了一个register_post_type('document', $args);函数,它会注册一种新的post类型。

转载于:https://www.cnblogs.com/phpcode/archive/2012/04/21/2522758.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值