创建一个新的 Post Type 需要使用 register_post_type 函数注,在你主题的 functions.php 文件下调用该函数:
register_post_type( $post_type, $args );
//$post_type 参数就是你自定义 Post Type 的名称。
function my_custom_post_product() {
$args = array();
register_post_type( \'product\', $args );
}
add_action( \'init\', \'my_custom_post_product\' );
参数很多,为了写教程方便,只列出比较常用的参数,大体结构如下:
function my_custom_post_site() {
$labels = array(
\'name\' => _x( \'网址导航\', \'post type 名称\' ),
\'singular_name\' => _x( \'网址\', \'post type 单个 item 时的名称,因为英文有复数\' ),
\'add_new\' => _x( \'新建网址\', \'添加新内容的链接名称\' ),
\'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,
\'description\' => \'网址信息\',
\'public\' => true,
\'menu_position\' => 5,
\'supports\' => array( \'title\', \'editor\', \'thumbnail\', \'excerpt\', \'comments\' ),
\'has_archive\' => true
);
register_post_type( \'site\', $args );
}
add_action( \'init\', \'my_custom_post_site\' );
将上面代码加到主题 functions.php
的最下面,进入后台你会发现多出了 site
选项,这样表示注册成功:

这时候我们可以新建 site
发表一篇电影类型的文章了。但是这样与文章类型基本相同,我们需要更多的自定义来完善我们的 site
类型。
添加分类功能需要使用函数 register_taxonomy,使用方法也很简单,跟注册 Post Type 函数类似,只不过多了一个参数用来指定对应的 Post Type :
register_taxonomy( $taxonomy, $object_type, $args );
就本例而言,可以配置如下常用参数:
function my_taxonomies_site() {
$labels = array(
\'name\' => _x( \'网址分类\', \'taxonomy 名称\' ),
\'singular_name\' => _x( \'网址分类\', \'taxonomy 单数名称\' ),
\'search_items\' => __( \'搜索网址分类\' ),
\'all_items\' => __( \'所有网址分类\' ),
\'parent_item\' => __( \'该网址分类的上级分类\' ),
\'parent_item_colon\' => __( \'该网址分类的上级分类:\' ),
\'edit_item\' => __( \'编辑网址分类\' ),
\'update_item\' => __( \'更新网址分类\' ),
\'add_new_item\' => __( \'添加新的网址分类\' ),
\'new_item_name\' => __( \'新网址分类\' ),
\'menu_name\' => __( \'网址分类\' ),
);
$args = array(
\'labels\' => $labels,
\'hierarchical\' => true,
);
register_taxonomy( \'sitecat\', \'site\', $args );
}
add_action( \'init\', \'my_taxonomies_site\', 0 );
添加到主题之后,我们看到出现了熟悉的文章分类功能,只不过上面的文案全部变成我们自定义的内容了:

为 Post Type 添加自定义 Meta Box
我们想要添加的电影类型不能仅仅只有正文内容,我们还需要额外添加一些 导演 之类的有关内容。那么就需要添加自定义 Meta Box,Meta Box 可以在文章发表页面中添加自定义的表单,编写文章的时候可以填写额外的信息然后在前端调用出来。
自定义 Meta Box 需要用到 add_meta_box 函数:
add_meta_box( $id, $title, $callback, $post_type, $context,$priority, $callback_args );
我们注册一个 Meta Box :
add_action( \'add_meta_boxes\', \'site_director\' );
function site_director() {
add_meta_box(
\'site_director\',
\'网址链接\',
\'site_director_meta_box\',
\'site\',
\'side\',
\'low\'
);
}
然后在配置参数里面指定了回调函数 site
_director_meta_box
,我们需要在这个函数里面创建表单:
function site_director_meta_box($post) {
// 创建临时隐藏表单,为了安全
wp_nonce_field( \'site_director_meta_box\', \'site_director_meta_box_nonce\' );
// 获取之前存储的值
$value = get_post_meta( $post->ID, \'_site_director\', true );
?>
<label for=\"site_director\"></label>
<input type=\"text\" id=\"site_director\" style=\"width:100%\" name=\"site_director\" value=\"<?php echo esc_attr( $value ); ?>\" placeholder=\"输入网址链接\" >
<?php
}
add_action( \'save_post\', \'site_director_save_meta_box\' );
function site_director_save_meta_box($post_id){
// 安全检查
// 检查是否发送了一次性隐藏表单内容(判断是否为第三者模拟提交)
if ( ! isset( $_POST[\'site_director_meta_box_nonce\'] ) ) {
return;
}
// 判断隐藏表单的值与之前是否相同
if ( ! wp_verify_nonce( $_POST[\'site_director_meta_box_nonce\'], \'site_director_meta_box\' ) ) {
return;
}
// 判断该用户是否有权限
if ( ! current_user_can( \'edit_post\', $post_id ) ) {
return;
}
// 判断 Meta Box 是否为空
if ( ! isset( $_POST[\'site_director\'] ) ) {
return;
}
$site_director = sanitize_text_field( $_POST[\'site_director\'] );
update_post_meta( $post_id, \'_site_director\', $site_director );
}
添加自定义字段:
add_action(\"manage_posts_custom_column\", \"site_custom_columns\");
add_filter(\"manage_edit-site_columns\", \"site_edit_columns\");
function site_custom_columns($column){
global $post;
switch ($column) {
case \"site_director\":
echo get_post_meta( $post->ID, \'_site_director\', true );
break;
}
}
function site_edit_columns($columns){
$columns[\'site_director\'] = \'网址\';
return $columns;
}
显示 Meta Box 内容
echo \'网址:\'.get_post_meta( get_the_ID(), \'_site_director\', true );
调用 WP_Query 高度自定义调用 Post Type 的内容
$args = array( \'post_type\' => \'site\', \'posts_per_page\' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
echo \'
<div class=\"entry-content\">\';
the_content();
echo \'</div>\';
endwhile;
1、本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2、分享目的仅供大家学习和交流,请不要用于商业用途!
3、本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
4、如有链接无法下载、失效或广告,请联系管理员处理!
5、本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
6、联系微信:A2022717!
袁博客 » WordPress PostType(自定义文章类型)功能介绍
2、分享目的仅供大家学习和交流,请不要用于商业用途!
3、本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
4、如有链接无法下载、失效或广告,请联系管理员处理!
5、本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
6、联系微信:A2022717!
袁博客 » WordPress PostType(自定义文章类型)功能介绍
常见问题FAQ
- 免费下载或者VIP会员专享资源能否直接商用?
- 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
- 提示下载完但解压或打开不了?
- 你们有qq群吗怎么加入?
play poker online for real money cialis 10mg drug cialis 10mg
buy generic azithromycin buy azithromycin 250mg for sale purchase neurontin pills
viagra for sale sildenafil 50mg tablet tadalafil 10mg generic
order prednisone 40mg pills prednisone 40mg usa order amoxil 250mg online cheap
sildenafil 50mg pills for men estradiol 2mg brand purchase lamotrigine online cheap
trazodone generic order trazodone 50mg generic buy sildenafil 50mg generic
buy sildenafil 100mg for sale sildenafil 100 mg where to buy cialis
tadalafil 5mg order cialis without prescription sildenafil us
order keflex without prescription erythromycin ca erythromycin price
cost tamsulosin 0.2mg purchase tamsulosin order spironolactone pill
online casinos real money play online roulette for fun free slots games
zantac 300mg sale order zantac sale generic celecoxib
colchicine order online casino usa real money best casino games
order sumatriptan 50mg sale sumatriptan 25mg price buy avodart 0.5mg pill
buy ozobax pill oral ketorolac buy toradol 10mg online cheap
losartan 50mg us nexium cost topiramate canada
motilium canada cost domperidone purchase flexeril without prescription
buy methotrexate 5mg for sale order reglan 20mg online order reglan without prescription
zyloprim for sale order rosuvastatin 20mg pills zetia 10mg oral
real cialis pharmacy prescription order plavix sale clopidogrel drug
buy xenical 60mg sale order acyclovir 400mg generic acyclovir us
purchase cialis pills viagra 100mg pills buy sildenafil 100mg sale
buy dapoxetine 90mg without prescription buy dapoxetine 60mg levothyroxine drug
tadalafil 5mg oral tadalafil 20mg tablet viagra 25mg
where can i buy an essay sildenafil pills 50mg sildenafil 100mg oral
order vardenafil 10mg buy pregabalin 75mg pill methylprednisolone tablet
essays help real money online blackjack blackjack free online
lopressor price order levitra 20mg online order vardenafil 10mg generic
buy generic omeprazole term papers writing service online roulette game
buy linezolid 600 mg pills best online casino for real money free spins no deposit uk
clozapine tablet order decadron 0,0,5 mg generic dexamethasone order
cheap norvasc 10mg cialis 40mg pill tadalafil for women
buy zyprexa 10mg nebivolol oral buy diovan pills
order metformin 500mg pill order lipitor 10mg online cheap tadalafil 20mg sale
purchase itraconazole generic tinidazole 300mg for sale order generic tindamax 500mg
buy cialis 20mg online betamethasone cheap buy clomipramine 25mg for sale
cheap tadalafil online order generic cialis 10mg rx pharmacy online viagra
zithromax 500mg for sale buy gabapentin pills order gabapentin 600mg without prescription
generic nootropil 800mg buy viagra sale viagra price
accutane 40mg ca prednisone 5mg tablet prednisone 20mg cheap
buy luvox 100mg pills buy ketoconazole 200mg buy glucotrol 10mg sale
cost provigil buy stromectol 6mg generic ivermectin cost in usa
medroxyprogesterone 5mg drug periactin 4mg drug brand cyproheptadine 4mg
buy avlosulfon online purchase nifedipine pills aceon cheap
revia 50mg usa revia 50 mg price abilify cheap
order tadalafil without prescription purchase phenazopyridine sale order symmetrel 100mg pills
tadalafil 10mg us viagra triangle restaurants viagra 25 mg
cheap salbutamol 100mcg purchase protonix generic viagra pills 100mg
zoloft online order buy zoloft 50mg pill sildenafil 100mg drug
zyban 150mg brand order cetirizine generic order quetiapine 50mg
isosorbide generic digoxin drug telmisartan cheap
buy prograf 1mg pills urso price buy ursodiol 150mg
fosamax 35mg cheap order fosamax for sale order pepcid 40mg generic
order hytrin pills sulfasalazine 500mg drug buy generic sulfasalazine
buspirone generic cheap phenytoin 100 mg cost ditropan
clonidine tablet buy catapres 0.1mg without prescription tiotropium bromide brand
order altace 5mg generic buy azelastine 10ml online cheap buy azelastine 10ml
generic doxycycline 100mg doxycycline 200mg generic furosemide 40mg brand
provigil 100mg for sale buy modafinil generic buy acetazolamide online cheap
accutane 10mg pill accutane canada stromectol usa
cialis 10mg generika rezeptfrei kaufen viagra kaufen für männer sildenafil für männer
prednisone 20mg price where to buy cialis over the counter viagra 100mg without prescription
cialis 20mg generique pas cher acheter 10mg gГ©nГ©rique cialis en france vrai sildenafil 100mg prix
anastrozole without prescription clarithromycin 500mg cheap buy sildenafil online
cheap tadalafil pills for ed buy sildenafil generic
order indocin 75mg pills order generic indomethacin 75mg cost trimox
accutane cost buy generic accutane 10mg zithromax 250mg cost
buy tadalis online order tadalafil 20mg voltaren 50mg cheap
lamotrigine drug vermox medication buy retin sale
order generic sildenafil 50mg buy sildenafil 100mg online cheap oral methocarbamol 500mg
order ampicillin online purchase erythromycin online cheap erythromycin 250mg generic
cost spironolactone 100mg oral diflucan 200mg buy generic fluconazole
avodart 0.5mg for sale cheap ondansetron zofran over the counter
levofloxacin oral buy levofloxacin 250mg online