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

Posted

tags:

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

Does not work at the moment. So don't copy it.
  1. <?php
  2.  
  3. function add_post_type($name, $args = array()) {
  4. add_action('init', function() use($name, $args) {
  5.  
  6. $upper = ucwords($name);
  7. $name = strtolower(str_replace(' ','_',$name));
  8.  
  9. $args = array_merge(
  10.  
  11. 'public'=> true,
  12. 'label' => "All $upper" . 's',
  13. 'labels' => array('add_new_item' => "Add New $upper"),
  14. 'support' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments', 'custom-fields'),
  15. 'taxonomies' => array('post_tag','category')
  16. ),
  17.  
  18. $args
  19.  
  20. );
  21.  
  22. register_post_type('$name', $args);
  23. });
  24.  
  25. }
  26.  
  27.  
  28. //now we create and register a taxonomy
  29. function add_taxonomy($name, $post_type, $args = array()) {
  30. $name = strtolower($name);
  31.  
  32. add_action('init', function() use($name, $post_type, $args) {
  33. $args = array_merge(
  34. 'label' => ucwords($name),
  35.  
  36. ),
  37. $args
  38. );
  39. register_taxonomy($name, $post_type, $args);
  40. });
  41. }
  42.  
  43. /************************************************************
  44.   Now we add the names of the custom post type and taxonomies
  45. *************************************************************/
  46.  
  47.  
  48. add_post_type('snippet', array(
  49. 'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments', 'custom-fields'),
  50. 'taxonomies' => array('post_tag')
  51. ));
  52.  
  53.  
  54. add_taxonomy('language', 'snippet');
  55.  
  56. /************************************************************
  57.   Creating Metaboxes
  58. *************************************************************/
  59.  
  60. add_action('add_meta_boxes', function() {
  61. add_meta_box(
  62. 'er_snippet_info',
  63. 'Snippet Info',
  64. 'er_snippet_info_cb',
  65. 'snippet',
  66. 'normal',
  67. 'high'
  68. );
  69. });
  70.  
  71. function er_snippet_info_cb() {
  72. global $post;
  73. $url = get_post_custom($post->ID);
  74.  
  75. ?>
  76.  
  77. <label for="er_associated_url">Associated URL: </label>
  78. <input type="text" name="er_associated_url" id="er_associated_url" value="<?php echo $url; ?>" />
  79.  
  80. <?php
  81. }
  82. add_action('save_post', function () {
  83. global $post;
  84. if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;
  85. //security check
  86. if ($_POST && !wp_verify_nonce($_POST['er_nonce'], _FILE_)) {
  87. if ( isset ($_POST['er_associated_url']) ) {
  88. update_post_meta($post->ID, 'er_associated_url', $_POST['er_associated_url']);
  89. }
  90. }
  91.  
  92. });
  93. ?>

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

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

PHP wordpress自定义帖子类型元框和字段

如何在选中的自定义元框中设置单选按钮?

如何在wordpress插件的自定义元框中保存多个复选框值?

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

Wordpress 自定义元框多张图片上传