Advertisement
febripratama

wp cust post

Nov 11th, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.01 KB | None | 0 0
  1. add_action( 'init', 'testimonial' );
  2.  
  3. function testimonial() {
  4.     register_post_type( 'testimonials',
  5.         array(
  6.             'labels' => array(
  7.                 'name' => 'testimonials',
  8.                 'singular_name' => 'testimonial',
  9.                 'add_new' => 'Add New',
  10.                 'add_new_item' => 'Add New testimonial',
  11.                 'edit' => 'Edit',
  12.                 'edit_item' => 'Edit testimonial',
  13.                 'new_item' => 'New testimonial',
  14.                 'view' => 'View',
  15.                 'view_item' => 'View new testimonial',
  16.                 'search_items' => 'Search testimonial',
  17.                 'not_found' => 'No testimonials found',
  18.                 'not_found_in_trash' => 'No testimonials in Trash',
  19.                 'parent' => 'Parent testimonial'
  20.             ),
  21.  
  22.             'public' => true,
  23.             'menu_position' => 16,
  24.             'supports' => array( 'title', 'editor', 'comments', 'thumbnail' ),
  25.             'taxonomies' => array( '' ),
  26.             'menu_icon' => true,
  27.             'has_archive' => true
  28.         )
  29.     );
  30. }
  31.  
  32. //nampilin customnya
  33. add_action( 'admin_init', 'testi' );
  34. function testi() {
  35.     add_meta_box( 'testimonial_meta_box',
  36.         'testimonials Details',
  37.         'display_testimonial_name_meta_box',
  38.         'testimonials', 'normal', 'high'
  39.     );
  40. }
  41.  
  42. //formnya
  43. function display_testimonial_name_meta_box( $testimonial ) {
  44.     // Ambil harga
  45.     $testimonial_name = esc_html( get_post_meta( $testimonial->ID, 'testimonial_name', true ) );
  46.  
  47.     echo    "<table>
  48.            <tr>
  49.                <td style='width: 100%'>Nama</td>
  50.                <td><input type='text' size='80' name='post_testimonial_name' value='". $testimonial_name."' /></td>
  51.            </tr>
  52.            </table>";
  53.  
  54. }
  55.  
  56. //custom field ke database
  57. add_action( 'save_post', 'add_testimonial_name_fields', 10, 2 );
  58.  
  59. //This function is executed when posts are saved or deleted from the admin panel
  60. function add_testimonial_name_fields( $testimonial_id, $testimonial ) {
  61.     // Check post type for post stuffs
  62.     if ( $testimonial->post_type == 'testimonials' ) {
  63.         // Store data in post meta table if present in post data
  64.         if ( isset( $_POST['testimonial_name'] ) && $_POST['testimonial_name'] != '' ) {
  65.             update_post_meta( $testimonial_id, 'testimonial_name', $_POST['testimonial_name'] );
  66.         }
  67.     }
  68. }
  69.  
  70.  
  71. //buat single post untuk ini
  72. add_filter( 'template_include', 'laman_testi', 1 );
  73. function laman_testi( $template_path ) {
  74.     if ( get_post_type() == 'testimonials' ) {
  75.         if ( is_single() ) {
  76.             // checks if the file exists in the theme first,
  77.             // otherwise serve the file from the plugin
  78.             if ( $theme_file = locate_template( array ( 'single-testimonial.php' ) ) ) {
  79.                 $template_path = $theme_file;
  80.             } else {
  81.                 $template_path = plugin_dir_path( __FILE__ ) . '/single-testimonial.php';
  82.             }
  83.         }
  84.     }
  85.     return $template_path;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement