PHP wordpress自定义帖子类型元框和字段
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP wordpress自定义帖子类型元框和字段相关的知识,希望对你有一定的参考价值。
<?php
/*custom post types */
add_action( 'init', 'create_post_types' );
function create_post_types() {
//vehicles
register_post_type( 'wel_vehicle',
array(
'labels' => array(
'name' => __( 'Vehicles' ),
'singular_name' => __( 'Vehicle' )
),
'public' => true,
'rewrite' => array('slug' => 'vehicles'),
'supports'=>array( 'title', 'editor', 'thumbnail', 'custom-fields', 'excerpt' )
)
);
//furniture
register_post_type( 'wel_furniture',
array(
'labels' => array(
'name' => __( 'Furniture' ),
'singular_name' => __( 'Furniture' )
),
'public' => true,
'rewrite' => array('slug' => 'furniture')
)
);
//machinery
register_post_type( 'wel_machinery',
array(
'labels' => array(
'name' => __( 'Machinery' ),
'singular_name' => __( 'machinery' )
),
'public' => true,
'rewrite' => array('slug' => 'machinery')
)
);
}
/*custom meta boxes */
add_action('add_meta_boxes','mycustom_meta_box');
//when post is saved
add_action('save_post', 'save_features');
function mycustom_meta_box(){
add_meta_box('v_feats',__('Vehicle Features','myplugin_text_domain'),
'mycustom_fields','wel_vehicle');
}
function mycustom_fields(){
//user nonce for verirfication
wp_nonce_field(plugin_basename(__FILE__),'myplugin_noncename');
//vehicle sale type-rent/sale
$v_type="<label for=\"vehicle_type\"><strong>For Sale or Rent</strong></label><br>";
$v_type.="<select name=\"sale_type\" id=sale_type>";
$v_type.="<option>For Sale</option>";
$v_type.="<option>To Rent</option>";
$v_type.="</select><br><br>";
//vehicle price
$v_type.="<label for=\"price\"><strong>Price</strong></label><br>";
$v_type.="<input type=\"text\" name=\"v_price\" /><br><br>";
//vehicle condition
$v_type.="<label for=\"condition\"><strong>Vehicle Condition</strong></label><br>";
$v_type.="<select name=\"v_condition\">";
$v_type.="<option>Used</option>";
$v_type.="<option>New</option>";
$v_type.="</select><br><br>";
//vehicle transmission
$v_type.="<label for=\"tranmission\"><strong>Vehicle Transmission</strong></label><br>";
$v_type.="<select name=\"v_transmission\">";
$v_type.="<option>Automatic</option>";
$v_type.="<option>Manual</option>";
$v_type.="</select><br><br>";
echo $v_type;
}
function save_features($post_id){
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename(__FILE__) )) {
return $post_id;
}
// verify if this is an auto save routine. If it is our form has not been submitted, so we dont want
// to do anything
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return $post_id;
// Check permissions
if ( 'page' == $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id ) )
return $post_id;
} else {
if ( !current_user_can( 'edit_post', $post_id ) )
return $post_id;
}
// OK, we're authenticated: we need to find and save the data
update_post_meta($post_id,$sale_type,$_POST['sale_type']);
}
/*custom taxonomies*/
//vehicle type taxom(from tommy's mamalia,carnivore')
//client can add other vehicle makes e.g hummer toyotajeep
// and query for single model
register_taxonomy('vehicle_make', 'wel_vehicle',
array('hierarchical' => false,
'label' => 'Vehicle Make',
'query_var' => true,
'rewrite' => true));
?>
以上是关于PHP wordpress自定义帖子类型元框和字段的主要内容,如果未能解决你的问题,请参考以下文章
WordPress - 无法从自定义帖子类型中的元框获取价值
Wordpress - 特色图像元框未显示在自定义帖子类型上