Advertisement
pusatdata

Cara Membuat Custom Post Type

Jan 24th, 2017
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. 1. Buat folder "product" di dalam folder template kita.
  2.  
  3. 2. Buat file post-type.php dan masukkan dalam folder product di atas.
  4.  
  5. 3. Panggil file tersebut dengan memasukkan kode berikut dalam file function.php:
  6. require get_template_directory() . '/products/post-type.php';
  7.  
  8. 4. Masukkan kode berikut dalam file post-type.php:
  9.  
  10. <?php
  11. function onphpid_post_type()
  12. {
  13. // add post-type
  14. register_post_type(
  15. 'onphpid_products',
  16. array(
  17. 'labels' => array(
  18. 'name' => __('Products', 'semi-toko-online'),
  19. 'singular_name' => __('Product', 'semi-toko-online'),
  20. 'add_new' => _x( 'Add New', 'product', 'semi-toko-online' ),
  21. 'add_new_item' => __( 'Add New Product', 'semi-toko-online' ),
  22. 'new_item' => __( 'New Product', 'twentytwelve' ),
  23. 'edit_item' => __( 'Edit Product', 'semi-toko-online' ),
  24. 'view_item' => __( 'View Product', 'semi-toko-online' ),
  25. 'all_items' => __( 'All Products', 'semi-toko-online' ),
  26. ),
  27. 'public' => true,
  28. 'supports' => array('title', 'editor', 'thumbnail'),
  29. 'has_archive' => true,
  30. 'rewrite' => array('slug'=>'product'),
  31. )
  32. );
  33. }
  34.  
  35. add_action('init', 'onphpid_post_type');
  36.  
  37. KETERANGAN:
  38. Setelah kalian menambahkan kode diatas kalian akan melihat menu Products di bawah menu Comments atau diatas menu Appearance.
  39. Pada tahap lanjutnya kita akan mengupah posisi dan icon agar sama dengan gambar pertama.
  40.  
  41. Untuk mengubah posisi menu Custom Post Type kalian bisa menggunakan "menu_position" => 100, agar Menu berada dibawah Settings.
  42. Letakkan kode berikut di bawah "rewrite" => array("slug" => "product"),"
  43.  
  44. ...
  45. 'public' => true,
  46. 'supports' => array('title', 'editor', 'thumbnail'),
  47. 'has_archive' => true,
  48. 'rewrite' => array('slug'=>'product'),
  49. 'menu_position' => 100, // code posisi
  50. ...
  51.  
  52. 5. Jika kalian ingin menempatkan diposisi lain kalian bisa melihat list berikut:
  53.  
  54. 5 – dibawah Posts
  55. 10 – dibawah Media
  56. 15 – dibawah Links
  57. 20 – dibawah Pages
  58. 25 – dibawah comments
  59. 60 – dibawah first separator
  60. 65 – dibawah Plugins
  61. 70 – dibawah Users
  62. 75 – dibawah Tools
  63. 80 – dibawah Settings
  64. 100 – dibawah second separator
  65.  
  66. Kemudian kita akan mengganti icon Menu Custom Post Type. Kita bisa mengubah Icon kita dengan menggunakan ‘menu_icon’, caranya dengan menambahkan 'menu_icon' => 'dashicons-cart' untuk icon keranjang. Letakkan kode seperti dibawah
  67.  
  68. ...
  69. 'public' => true,
  70. 'supports' => array('title', 'editor', 'thumbnail'),
  71. 'has_archive' => true,
  72. 'rewrite' => array('slug'=>'product'),
  73. 'menu_position' => 100,
  74. 'menu_icon' => 'dashicons-cart' // mengubah icon
  75. ...
  76.  
  77. KETERANGAN:
  78. jika kalian ini mengganti dengan icon yang lain kalian bisa check di https://developer.wordpress.org/resource/dashicons/
  79.  
  80. 6. Kurang Lebih Kode keseluruhan yang ada di post-type.php adalah sebagai berikut :
  81.  
  82. <?php
  83. /**
  84. * tutorial membuat semi toko online
  85. * @package https://www.onphpid.com/category/membuat-semi-toko-online
  86. * @see : https://www.onphpid.com/langkah-awal-membuat-semi-toko-online-dengan-wordpress.html
  87. */
  88. function onphpid_post_type()
  89. {
  90. // add post-type
  91. register_post_type(
  92. 'onphpid_products',
  93. array(
  94. 'labels' => array(
  95. 'name' => __('Products', 'semi-toko-online'),
  96. 'singular_name' => __('Product', 'semi-toko-online'),
  97. 'add_new' => _x( 'Add New', 'product', 'semi-toko-online' ),
  98. 'add_new_item' => __( 'Add New Product', 'semi-toko-online' ),
  99. 'new_item' => __( 'New Product', 'semi-toko-online' ),
  100. 'edit_item' => __( 'Edit Product', 'semi-toko-online' ),
  101. 'view_item' => __( 'View Product', 'semi-toko-online' ),
  102. 'all_items' => __( 'All Products', 'semi-toko-online' ),
  103. ),
  104. 'public' => true,
  105. 'supports' => array('title', 'editor', 'thumbnail'),
  106. 'has_archive' => true,
  107. 'rewrite' => array('slug'=>'product'),
  108. 'menu_position' => 100,
  109. 'menu_icon' => 'dashicons-cart'
  110. )
  111. );
  112. }
  113.  
  114. add_action('init', 'onphpid_post_type');
  115.  
  116. KETERANGAN:
  117. Sekian dulu Tutorial Membuat Semi Toko Online Dengan WordPress. Selebihnya kalian bisa baca-baca di link berikut:
  118.  
  119. https://codex.wordpress.org/Post_Types
  120.  
  121. https://codex.wordpress.org/Function_Reference/register_post_type
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement