https://www.smashingmagazine.com/2012/11/complete-guide-custom-post-types/
このサイトを元にfunctions.phpに次のコードを追記しました。
php
1//カスタム投稿タイプ 2function my_custom_post_product() { 3 $labels = array( 4 'name' => _x( 'Products', 'post type general name' ), 5 'singular_name' => _x( 'Product', 'post type singular name' ), 6 'add_new' => _x( 'Add New', 'book' ), 7 'add_new_item' => __( 'Add New Product' ), 8 'edit_item' => __( 'Edit Product' ), 9 'new_item' => __( 'New Product' ), 10 'all_items' => __( 'All Products' ), 11 'view_item' => __( 'View Product' ), 12 'search_items' => __( 'Search Products' ), 13 'not_found' => __( 'No products found' ), 14 'not_found_in_trash' => __( 'No products found in the Trash' ), 15 'parent_item_colon' => '', 16 'menu_name' => 'Products' 17 ); 18 $args = array( 19 'labels' => $labels, 20 'description' => 'Holds our products and product specific data', 21 'public' => true, 22 'menu_position' => 5, 23 'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ), 24 'has_archive' => true, 25 ); 26 register_post_type( 'product', $args ); 27} 28add_action( 'init', 'my_custom_post_product' ); 29 30//カスタムタクソノミー 31function my_taxonomies_product() { 32 $labels = array( 33 'name' => _x( 'Product Categories', 'taxonomy general name' ), 34 'singular_name' => _x( 'Product Category', 'taxonomy singular name' ), 35 'search_items' => __( 'Search Product Categories' ), 36 'all_items' => __( 'All Product Categories' ), 37 'parent_item' => __( 'Parent Product Category' ), 38 'parent_item_colon' => __( 'Parent Product Category:' ), 39 'edit_item' => __( 'Edit Product Category' ), 40 'update_item' => __( 'Update Product Category' ), 41 'add_new_item' => __( 'Add New Product Category' ), 42 'new_item_name' => __( 'New Product Category' ), 43 'menu_name' => __( 'Product Categories' ), 44 ); 45 $args = array( 46 'labels' => $labels, 47 'hierarchical' => true, 48 ); 49 register_taxonomy( 'product_category', 'product', $args ); 50} 51add_action( 'init', 'my_taxonomies_product', 0 ); 52 53//メタボックス定義 54add_action( 'add_meta_boxes', 'product_price_box' ); 55function product_price_box() { 56 add_meta_box( 57 'product_price_box', 58 __( 'Product Price', 'myplugin_textdomain' ), 59 'product_price_box_content', 60 'product', 61 'side', 62 'high' 63 ); 64} 65 66//メタボックス内容の定義 67function product_price_box_content( $post ) { 68 wp_nonce_field( plugin_basename( __FILE__ ), 'product_price_box_content_nonce' ); 69 echo '<label for="product_price"></label>'; 70 echo '<input type="text" id="product_price" name="product_price" placeholder="enter a price" />'; 71} 72 73//送信されたデータ処理 74add_action( 'save_post', 'product_price_box_save' ); 75function product_price_box_save( $post_id ) { 76 77 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 78 return; 79 80 if ( !wp_verify_nonce( $_POST['product_price_box_content_nonce'], plugin_basename( __FILE__ ) ) ) 81 return; 82 83 if ( 'page' == $_POST['post_type'] ) { 84 if ( !current_user_can( 'edit_page', $post_id ) ) 85 return; 86 } else { 87 if ( !current_user_can( 'edit_post', $post_id ) ) 88 return; 89 } 90 $product_price = $_POST['product_price']; 91 update_post_meta( $post_id, 'product_price', $product_price ); 92}
metaboxが作られたことは確認できたのですが、送信されたデータ処理で混乱しています。
データベースに送信されているということでしょうか。後、ここの部分で
echo '<input type="text" id="product_price" name="product_price" placeholder="enter a price" />';
value=を加えて送信された値をそこに入れたいのですが、どう記述すればいいか分かりませんでした。
value="get_post_meta($post->ID, $key, true)"としましたが、
そのままget_post_meta($post_ID, $key, true)が表示されてしまい、50等、入力された値が格納されていませんでした。$keyの部分もなんと入れればいいか分からなかったです。
以上2点について、混乱していて分かりづらい質問だと思いますが、どなたかアドバイスおねがいします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。