Advertisement
pusatdata

Meta Box Khusus Buku

Mar 13th, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.11 KB | None | 0 0
  1. function.php
  2. ==================
  3.  
  4. add_action('meta_box','new_meta_boxes');
  5. //META BOX
  6. /*
  7. ---------------------
  8. Custom Field
  9. ---------------------
  10. */
  11.  
  12. $new_meta_boxes =
  13. array(
  14. "kotak-isian-01" => array(
  15. "name" => "penulis",
  16. "title" => "Penulis",
  17. "description" => ""),
  18. "kotak-isian-02" => array(
  19. "name" => "cetakan",
  20. "title" => "Cetakan",
  21. "description" => ""),
  22. "kotak-isian-03" => array(
  23. "name" => "verifikasi",
  24. "title" => "Verifikasi",
  25. "description" => ""),
  26. "kotak-isian-04" => array(
  27. "name" => "link_verifikasi",
  28. "title" => "Link Verifikasi",
  29. "description" => ""),
  30. "kotak-isian-05" => array(
  31. "name" => "label",
  32. "title" => "URL Image Label",
  33. "description" => ""),
  34. );
  35. //end of array
  36.  
  37.  
  38. //now we begin a function to create the html that comprise the new meta boxes
  39. //below you can edit/add inline css if you want to change how the label and description appear on the admin page
  40. //worth testing, but not important to manipulate this portion of the code unless you have a super specific objective in mind.
  41. function new_meta_boxes() {
  42. global $post, $new_meta_boxes;
  43.  
  44. foreach($new_meta_boxes as $meta_box) {
  45. $meta_box_value = get_post_meta($post->ID, $meta_box['name'], true);
  46.  
  47. if($meta_box_value == "")
  48. $meta_box_value = $meta_box['std'];
  49.  
  50. echo'<input type="hidden" name="'.$meta_box['name'].'_noncename" id="'.$meta_box['name'].'_noncename"
  51. value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';
  52.  
  53. echo'<label style="font-weight: bold; display: block; padding: 5px 0 2px 2px"
  54. for="'.$meta_box['name'].'">'.$meta_box['title'].'</label>';
  55.  
  56. echo'<input type="text" name="'.$meta_box['name'].'" value="'.$meta_box_value.'" size="55" /><br />';
  57.  
  58. echo'<p><label for="'.$meta_box['name'].'">'.$meta_box['description'].'</label></p>';
  59. }
  60. }//end of new_meta_boxes() function
  61.  
  62.  
  63. //another function begins here. This is what actually creates the meta box. The one you see on the admin page.
  64. //you should edit the title for your meta box below. don't worry about anything else
  65. function create_meta_box() {
  66. global $theme_name;
  67. if ( function_exists('add_meta_box') ) {
  68. add_meta_box( 'new-meta-boxes', 'DETAIL BUKU', 'new_meta_boxes', 'post', 'normal', 'high' );
  69. }
  70. }//end of create_meta_box() function
  71.  
  72. //the third and most important function. this is what effectively saves your meta data into your database.
  73. //DO NOT EDIT unless you have expert php skills or advice.
  74. function save_postdata( $post_id ) {
  75. global $post, $new_meta_boxes;
  76.  
  77. foreach($new_meta_boxes as $meta_box) {
  78. // Verify
  79. if ( !wp_verify_nonce( $_POST[$meta_box['name'].'_noncename'], plugin_basename(__FILE__) )) {
  80. return $post_id;
  81. }
  82.  
  83. if ( 'page' == $_POST['post_type'] ) {
  84. if ( !current_user_can( 'edit_page', $post_id ))
  85. return $post_id;
  86. } else {
  87. if ( !current_user_can( 'edit_post', $post_id ))
  88. return $post_id;
  89. }
  90.  
  91. $data = $_POST[$meta_box['name']];
  92.  
  93. if(get_post_meta($post_id, $meta_box['name']) == "")
  94. add_post_meta($post_id, $meta_box['name'], $data, true);
  95. elseif($data != get_post_meta($post_id, $meta_box['name'], true))
  96. update_post_meta($post_id, $meta_box['name'], $data);
  97. elseif($data == "")
  98. delete_post_meta($post_id, $meta_box['name'], get_post_meta($post_id, $meta_box['name'], true));
  99. }
  100. }//end of save_postdata () function
  101.  
  102. //these are action hooks that place your special functions defined above onto the admin page.
  103. add_action('admin_menu', 'create_meta_box');
  104. add_action('save_post', 'save_postdata');
  105.  
  106.  
  107.  
  108. single.php
  109. ================
  110. <table>
  111. <tr>
  112. <td style="text-align: center;vertical-align: middle;" width="248" colspan="4">
  113.  
  114.  
  115. <img src="<?php echo get_post_meta($post->ID, "label", true);?>" style="width:150px;align:center;" onError="this.src='/belum-terverifikasi.png';"/>
  116. <h2 class="title"><?php the_title(); ?></h2>
  117. </td>
  118. </tr>
  119.  
  120. <tr>
  121. <td style="text-align: center;vertical-align: middle; width:150px" rowspan="6"><img src="/img/cover/<?php echo get_the_ID(); ?>.jpg" width="150px" class="bw" onError="this.src='/no-cover.png';"/></td>
  122.  
  123. <td align="right" class="databuku kiri" style="vertical-align: middle;" >Penulis</td>
  124. <td align="center" class="databuku titikdua">:</td>
  125. <td class="databuku kanan"><?php echo get_post_meta($post->ID, "penulis", true);?></td>
  126. </tr>
  127. <tr>
  128. <td align="right" class="databuku kiri" style="vertical-align: middle;">Penerbit</td>
  129. <td align="center" class="databuku titikdua">:</td>
  130. <td class="databuku kanan"><?php the_category(', '); ?></td>
  131. </tr>
  132. <tr>
  133. <td align="right" class="databuku kiri" style="vertical-align: middle;">Terbit</td>
  134. <td align="center" class="databuku titikdua">:</td>
  135. <td class="databuku kanan"><?php the_time('Y'); ?></td>
  136. </tr>
  137. <tr>
  138. <td align="right" class="databuku kiri" style="vertical-align: middle;">Cetakan</td>
  139. <td align="center" class="databuku titikdua">:</td>
  140. <td class="databuku kanan"><?php echo get_post_meta($post->ID, "cetakan", true);?></td>
  141. </tr>
  142. <tr>
  143. <td align="right" class="databuku kiri" style="vertical-align: middle;">Status MUBI</td>
  144. <td align="center" class="databuku titikdua">:</td>
  145. <td class="databuku kanan"><?php
  146. $id_verifikasi = get_post_meta( get_the_ID(), 'verifikasi', true);
  147. if( ! empty( $id_verifikasi ) ) {
  148. echo '' . $id_verifikasi . '';
  149. }
  150. ?></td>
  151. </tr>
  152. </table>
  153.  
  154.  
  155.  
  156. SATU KODE PHP UNTUK MENAMPILKAN CUSTOM FILE
  157. ;
  158.  
  159. single.php
  160. ================
  161. <h4 style="margin:0 8px 6px 0px; padding-left:20px;">Data Buku</h4>
  162. <table>
  163. <?php
  164. $my_title = get_the_title();
  165. $my_date = get_post_meta( get_the_ID(), 'date', true);
  166. $my_pentahqiq = get_post_meta( get_the_ID(), 'pentahqiq', true);
  167. $my_sampul = get_post_meta( get_the_ID(), 'sampul', true);
  168. $my_kertas = get_post_meta( get_the_ID(), 'kertas', true);
  169. $my_jilid = get_post_meta( get_the_ID(), 'jilid', true);
  170. $my_pembahasan = get_post_meta( get_the_ID(), 'pembahasan', true);
  171. $my_publisher = get_the_category();
  172. $my_penerbit = get_the_tags();
  173.  
  174. // Here we save the "Genre" terms in a variable.
  175. $penulis_terms = get_the_term_list( $post->ID, 'penulis', '', ', ', ' ' );
  176.  
  177. if( ! empty( $my_title ) ) {
  178. echo '<tr><td align="right" class="style" width="210"><b>Judul</td><td align="center" class="style">:</td></b><td class="style"> ' . $my_title . '</td></tr>';
  179. }
  180.  
  181. // Here "Penulis" are displayed if not empty.
  182. if( ! empty( $penulis_terms ) ) {
  183. echo '<tr><td align="right" class="style"><b>Penulis</td><td align="center" class="style">:</td></b><td class="style"> ' . $penulis_terms . ' </td></tr>';
  184. }
  185.  
  186. if( ! empty( $my_pentahqiq ) ) {
  187. echo '<tr><td align="right" class="style"><b>Pentahqiq</td><td align="center" class="style">:</td></b><td class="style"> ' . $my_pentahqiq . '</td></tr>';
  188. }
  189.  
  190. if( ! empty( $my_sampul ) ) {
  191. echo '<tr><td align="right" class="style"><b>Sampul</td><td align="center" class="style">:</td></b><td class="style"> ' . $my_sampul . '</td></tr>';
  192. }
  193.  
  194. if( ! empty( $my_kertas ) ) {
  195. echo '<tr><td align="right" class="style"><b>Kertas</td><td align="center" class="style">:</td></b><td class="style"> ' . $my_kertas . '</td></tr>';
  196. }
  197.  
  198. if( ! empty( $my_penerbit ) ) {
  199. $tag_links = array();
  200. foreach($my_penerbit as $tag) {
  201. $tag_links[] = '<a href="'.get_tag_link($tag).'">'.$tag->name.'</a>';
  202. }
  203.  
  204. if( ! empty( $my_jilid ) ) {
  205. echo '<tr><td align="right" class="style"><b>Jilid</td><td align="center" class="style">:</td></b><td class="style"> ' . $my_jilid . '</td></tr>';
  206. }
  207.  
  208. if( ! empty( $my_pembahasan ) ) {
  209. echo '<tr><td align="right" class="style"><b>Pembahasan</td><td align="center" class="style">:</td></b><td class="style"> ' . $my_pembahasan . '</td></tr>';
  210. }
  211.  
  212. ?>
  213. </table>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement