Advertisement
pusatdata

WP: Manually Creating Custom Taxonomies - Tags

Oct 7th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. To create a non-hierarchical custom taxonomy like Tags, add this code in your theme’s functions.php or in a site-specific plugin:
  2.  
  3. =================
  4.  
  5. //hook into the init action and call create_topics_nonhierarchical_taxonomy when it fires
  6.  
  7. add_action( 'init', 'create_topics_nonhierarchical_taxonomy', 0 );
  8.  
  9. function create_topics_nonhierarchical_taxonomy() {
  10.  
  11. // Labels part for the GUI
  12.  
  13. $labels = array(
  14. 'name' => _x( 'Topics', 'taxonomy general name' ),
  15. 'singular_name' => _x( 'Topic', 'taxonomy singular name' ),
  16. 'search_items' => __( 'Search Topics' ),
  17. 'popular_items' => __( 'Popular Topics' ),
  18. 'all_items' => __( 'All Topics' ),
  19. 'parent_item' => null,
  20. 'parent_item_colon' => null,
  21. 'edit_item' => __( 'Edit Topic' ),
  22. 'update_item' => __( 'Update Topic' ),
  23. 'add_new_item' => __( 'Add New Topic' ),
  24. 'new_item_name' => __( 'New Topic Name' ),
  25. 'separate_items_with_commas' => __( 'Separate topics with commas' ),
  26. 'add_or_remove_items' => __( 'Add or remove topics' ),
  27. 'choose_from_most_used' => __( 'Choose from the most used topics' ),
  28. 'menu_name' => __( 'Topics' ),
  29. );
  30.  
  31. // Now register the non-hierarchical taxonomy like tag
  32.  
  33. register_taxonomy('topics','post',array(
  34. 'hierarchical' => false,
  35. 'labels' => $labels,
  36. 'show_ui' => true,
  37. 'show_admin_column' => true,
  38. 'update_count_callback' => '_update_post_term_count',
  39. 'query_var' => true,
  40. 'rewrite' => array( 'slug' => 'topic' ),
  41. ));
  42. }
  43.  
  44. Kode menampilkan di web, masukkan ke single.php:
  45. <?php the_terms( $post->ID, 'topics', 'Topics: ', ', ', ' ' ); ?>
  46.  
  47. kunci: http://www.wpbeginner.com/wp-tutorials/create-custom-taxonomies-wordpress/
  48.  
  49. =============================================
  50. =============================================
  51. INI YANG BISA
  52. =============================================
  53. =============================================
  54.  
  55. //create a function that will attach our new 'penulis' taxonomy to the 'post' post type
  56.  
  57. function add_penulis_taxonomy_to_post(){
  58.  
  59. //set the name of the taxonomy
  60. $taxonomy = 'penulis';
  61. //set the post types for the taxonomy
  62. $object_type = 'post';
  63.  
  64. //populate our array of names for our taxonomy
  65. $labels = array(
  66. 'name' => 'penulis',
  67. 'singular_name' => 'penulis',
  68. 'search_items' => 'Search penulis',
  69. 'all_items' => 'All penulis',
  70. 'parent_item' => 'Parent penulis',
  71. 'parent_item_colon' => 'Parent penulis:',
  72. 'update_item' => 'Update penulis',
  73. 'edit_item' => 'Edit penulis',
  74. 'add_new_item' => 'Add New penulis',
  75. 'new_item_name' => 'New penulis Name',
  76. 'menu_name' => 'penulis'
  77. );
  78.  
  79. //define arguments to be used
  80. $args = array(
  81. 'labels' => $labels,
  82. 'hierarchical' => false,
  83. 'show_ui' => true,
  84. 'how_in_nav_menus' => true,
  85. 'public' => true,
  86. 'show_admin_column' => true,
  87. 'query_var' => true,
  88. 'rewrite' => array('slug' => 'penulis')
  89. );
  90.  
  91. //call the register_taxonomy function
  92. register_taxonomy($taxonomy, $object_type, $args);
  93. }
  94. add_action('init','add_penulis_taxonomy_to_post');
  95.  
  96. Kode menampilkan di web, masukkan ke single.php:
  97. <?php the_terms( $post->ID, 'topics', 'Topics: ', ', ', ' ' ); ?>
  98.  
  99. DIambil dar: https://generatewp.com/taxonomy/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement