自定义发布类型分类法和消息

Posted

tags:

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

This gives the code for creating a custom post type with all the necessary labels and even custom message support, and it gives the code for a custom taxonomy with that post type.
  1. // The register_post_type() function is not to be used before the 'init'.
  2. add_action( 'init', 'my_custom_init' );
  3.  
  4. /* Here's how to create your customized labels */
  5. function my_custom_init() {
  6. $labels = array(
  7. 'name' => _x( 'Portfolio Galleries', 'post type general name' ), // Tip: _x('') is used for localization
  8. 'singular_name' => _x( 'Portfolio Gallery', 'post type singular name' ),
  9. 'add_new' => _x( 'Add New', 'Portfolio Gallery' ),
  10. 'add_new_item' => __( 'Add New Portfolio Gallery' ),
  11. 'edit_item' => __( 'Edit Portfolio Gallery' ),
  12. 'new_item' => __( 'New Portfolio Gallery' ),
  13. 'view_item' => __( 'View Portfolio Gallery' ),
  14. 'search_items' => __( 'Search Portfolio Galleries' ),
  15. 'not_found' => __( 'No Portfolio Galleries found' ),
  16. 'not_found_in_trash' => __( 'No Portfolio Galleries found in Trash' ),
  17. 'parent_item_colon' => ''
  18. );
  19.  
  20. // Create an array for the $args
  21. $args = array( 'labels' => $labels,
  22. 'public' => true,
  23. 'publicly_queryable' => true,
  24. 'show_ui' => true,
  25. 'query_var' => true,
  26. 'rewrite' => true,
  27. 'capability_type' => 'post',
  28. 'hierarchical' => true,
  29. 'menu_position' => null,
  30. 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'page-attributes' )
  31. );
  32.  
  33. register_post_type( 'portfolio-gallery', $args ); /* Register it and move on */
  34. }
  35. //add a custom message to the post message function
  36.  
  37. add_filter('post_updated_messages', 'gallery_updated_messages');
  38. function gallery_updated_messages( $messages ) {
  39.  
  40. $messages['portfolio-gallery'] = array(
  41. 0 => '', // Unused. Messages start at index 1.
  42. 1 => sprintf( __('Gallery updated. <a href="%s">View Gallery</a>'), esc_url( get_permalink($post_ID) ) ),
  43. 2 => __('Custom field updated.'),
  44. 3 => __('Custom field deleted.'),
  45. 4 => __('Gallery updated.'),
  46. /* translators: %s: date and time of the revision */
  47. 5 => isset($_GET['revision']) ? sprintf( __('Gallery restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
  48. 6 => sprintf( __('Gallery published. <a href="%s">View Gallery</a>'), esc_url( get_permalink($post_ID) ) ),
  49. 7 => __('Gallery saved.'),
  50. 8 => sprintf( __('Gallery submitted. <a target="_blank" href="%s">Preview Gallery</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
  51. 9 => sprintf( __('Gallery scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview Gallery</a>'),
  52. // translators: Publish box date format, see http://php.net/date
  53. date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
  54. 10 => sprintf( __('Gallery draft updated. <a target="_blank" href="%s">Preview Gallery</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
  55. );
  56.  
  57. return $messages;
  58. }
  59. // hook into the init action and call create_gallery_taxonomies() when it fires
  60. add_action( 'init', 'create_gallery_taxonomies', 0 );
  61. function create_gallery_taxonomies() {
  62.  
  63. // Add new taxonomy, hierarchical
  64. $labels = array(
  65. 'name' => _x( 'Categories', 'taxonomy general name' ),
  66. 'singular_name' => _x( 'Category', 'taxonomy singular name' ),
  67. 'search_items' => __( 'Search Categories' ),
  68. 'popular_items' => __( 'Popular Categories' ),
  69. 'all_items' => __( 'All Categories' ),
  70. 'parent_item' => null,
  71. 'parent_item_colon' => null,
  72. 'edit_item' => __( 'Edit Category' ),
  73. 'update_item' => __( 'Update Category' ),
  74. 'add_new_item' => __( 'Add New Category' ),
  75. 'new_item_name' => __( 'New Category Name' ),
  76. 'separate_items_with_commas' => __( 'Separate categories with commas' ),
  77. 'add_or_remove_items' => __( 'Add or remove categories' ),
  78. 'choose_from_most_used' => __( 'Choose from the most used categories' )
  79. );
  80. register_taxonomy( 'gallery-category', 'portfolio-gallery', array(
  81. 'hierarchical' => true,
  82. 'labels' => $labels, /* NOTICE: the $labels variable here */
  83. 'show_ui' => true,
  84. 'query_var' => true,
  85. 'rewrite' => array( 'slug' => 'gallery-category' ),
  86. ));
  87. }

以上是关于自定义发布类型分类法和消息的主要内容,如果未能解决你的问题,请参考以下文章

自定义帖子类型和分类法分页 404 错误

在代码片段中包含类型转换

在片段中创建自定义列表视图时出错。必需的活动,找到的片段

WordPress 自定义帖子类型前端

仅显示特定自定义帖子类型的自定义分类计数

Wordpress 自定义帖子类型和自定义分类