wordpress自定义post类型元框和字段

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了wordpress自定义post类型元框和字段相关的知识,希望对你有一定的参考价值。

  1. <?php
  2.  
  3.  
  4.  
  5.  
  6.  
  7. /*custom post types */
  8. add_action( 'init', 'create_post_types' );
  9. function create_post_types() {
  10.  
  11. //vehicles
  12. register_post_type( 'wel_vehicle',
  13. 'labels' => array(
  14. 'name' => __( 'Vehicles' ),
  15. 'singular_name' => __( 'Vehicle' )
  16. ),
  17. 'public' => true,
  18. 'rewrite' => array('slug' => 'vehicles'),
  19. 'supports'=>array( 'title', 'editor', 'thumbnail', 'custom-fields', 'excerpt' )
  20. )
  21. );
  22.  
  23.  
  24. //furniture
  25. register_post_type( 'wel_furniture',
  26. 'labels' => array(
  27. 'name' => __( 'Furniture' ),
  28. 'singular_name' => __( 'Furniture' )
  29. ),
  30. 'public' => true,
  31. 'rewrite' => array('slug' => 'furniture')
  32. )
  33. );
  34.  
  35. //machinery
  36. register_post_type( 'wel_machinery',
  37. 'labels' => array(
  38. 'name' => __( 'Machinery' ),
  39. 'singular_name' => __( 'machinery' )
  40. ),
  41. 'public' => true,
  42. 'rewrite' => array('slug' => 'machinery')
  43. )
  44. );
  45. }
  46.  
  47.  
  48. /*custom meta boxes */
  49. add_action('add_meta_boxes','mycustom_meta_box');
  50.  
  51. //when post is saved
  52. add_action('save_post', 'save_features');
  53.  
  54.  
  55. function mycustom_meta_box(){
  56. add_meta_box('v_feats',__('Vehicle Features','myplugin_text_domain'),
  57. 'mycustom_fields','wel_vehicle');
  58.  
  59. }
  60.  
  61. function mycustom_fields(){
  62. //user nonce for verirfication
  63. wp_nonce_field(plugin_basename(__FILE__),'myplugin_noncename');
  64.  
  65. //vehicle sale type-rent/sale
  66. $v_type="<label for="vehicle_type"><strong>For Sale or Rent</strong></label><br>";
  67. $v_type.="<select name="sale_type" id=sale_type>";
  68. $v_type.="<option>For Sale</option>";
  69. $v_type.="<option>To Rent</option>";
  70. $v_type.="</select><br><br>";
  71.  
  72. //vehicle price
  73. $v_type.="<label for="price"><strong>Price</strong></label><br>";
  74. $v_type.="<input type="text" name="v_price" /><br><br>";
  75.  
  76. //vehicle condition
  77. $v_type.="<label for="condition"><strong>Vehicle Condition</strong></label><br>";
  78. $v_type.="<select name="v_condition">";
  79. $v_type.="<option>Used</option>";
  80. $v_type.="<option>New</option>";
  81. $v_type.="</select><br><br>";
  82.  
  83.  
  84. //vehicle transmission
  85. $v_type.="<label for="tranmission"><strong>Vehicle Transmission</strong></label><br>";
  86. $v_type.="<select name="v_transmission">";
  87. $v_type.="<option>Automatic</option>";
  88. $v_type.="<option>Manual</option>";
  89. $v_type.="</select><br><br>";
  90.  
  91. echo $v_type;
  92. }
  93. function save_features($post_id){
  94.  
  95. // verify this came from the our screen and with proper authorization,
  96. // because save_post can be triggered at other times
  97.  
  98. if ( !wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename(__FILE__) )) {
  99. return $post_id;
  100. }
  101. // verify if this is an auto save routine. If it is our form has not been submitted, so we dont want
  102. // to do anything
  103. if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return $post_id;
  104.  
  105. // Check permissions
  106. if ( 'page' == $_POST['post_type'] ) {
  107. if ( !current_user_can( 'edit_page', $post_id ) )
  108. return $post_id;
  109. } else {
  110. if ( !current_user_can( 'edit_post', $post_id ) )
  111. return $post_id;
  112. }
  113.  
  114. // OK, we're authenticated: we need to find and save the data
  115.  
  116. update_post_meta($post_id,$sale_type,$_POST['sale_type']);
  117.  
  118. }
  119.  
  120.  
  121.  
  122. /*custom taxonomies*/
  123.  
  124. //vehicle type taxom(from tommy's mamalia,carnivore')
  125. //client can add other vehicle makes e.g hummer toyotajeep
  126. // and query for single model
  127. register_taxonomy('vehicle_make', 'wel_vehicle',
  128. array('hierarchical' => false,
  129. 'label' => 'Vehicle Make',
  130. 'query_var' => true,
  131. 'rewrite' => true));
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142. ?>

以上是关于wordpress自定义post类型元框和字段的主要内容,如果未能解决你的问题,请参考以下文章

WordPress - 无法从自定义帖子类型中的元框获取价值

带有元框字段的自定义文章类型

自定义元框 Save_Post 回调未在 functions.php 中触发

Wordpress - 特色图像元框未显示在自定义帖子类型上

wordpress 元框中的多个复选框

从购物车表单复选框返回字符串(是/否)到 wordpress 元框