カスタム投稿ページに作成した、カスタムフィールドの値が全て同じになってしまう
現在、WordPressのサイトを作成しており、function.phpでカスタム投稿ページにカスタムフィールドを作成しました。
カスタムフィールドの値が全て最新の投稿の値になってしまうので、困っています。
何か不足している情報があれば教えてください。
例)
1記事目のカスタムフィールドに[チョコレート]と入力し更新。
2記事目のカスタムフィールドに[ドーナツ]と入力し更新すると、1記事目に[チョコレート]と入力した部分が[ドーナツ]に変更されます。
function.phpのソースコード
// カスタムフィールドの追加 add_action( 'admin_menu', 'add_custom_field' ); function add_custom_field() { add_meta_box( 'custom-item_sweets', 'お菓子の種類', 'create_item_sweets', 'product', 'normal'); } // カスタムフィールドのHTMLを追加する時の処理 function create_item_sweets() { $keyname = 'item_sweets'; global $post; $get_value = get_post_meta( $post->ID, $keyname, true ); wp_nonce_field( 'action-' . $keyname, 'nonce-' . $keyname ); echo '<input name="' . $keyname . '" value="' . $get_value . '">'; } // カスタムフィールドの保存 add_action( 'save_post', 'save_custom_field' ); function save_custom_field( $post_id ) { $custom_fields = ['item_sweets']; foreach( $custom_fields as $d ) { if ( isset( $_POST['nonce-' . $d] ) && $_POST['nonce-' . $d] ) { if( check_admin_referer( 'action-' . $d, 'nonce-' . $d ) ) { if( isset( $_POST[$d] ) && $_POST[$d] ) { update_post_meta( $post_id, $d, $_POST[$d] ); } else { delete_post_meta( $post_id, $d, get_post_meta( $post_id, $d, true ) ); } } } } }
taxonomy.phpのソースコード
<?php query_posts($query_string . "&pst_type=product_cat&posts_per_page=10&paged='.$paged"); ?> <?php if (have_posts()) : while (have_posts()) : the_post(); ?> //コンテンツ <?php endwhile; else: ?> <p>記事はまだありません。</p> <?php endif; ?>
試したこと
記事にタイトルを入れて投稿しましたが、タイトルは同じになりませんでした。
カスタムフィールドを作成した際に参考にしたサイト
http://cly7796.net/wp/cms/add-custom-fields-with-functions-php/
ちなみに上記サイトもカスタムフィールドの内容は全て同じになりました、、、
回答1件
あなたの回答
tips
プレビュー