カスタムフィールドの入力と反映
カスタムフィールドを使えるようにしたので
現在テーブルに入力した値をこちらに入力します
今回は hello world の投稿の下に追加された
カスタムフィールドテンプレートに入力します
入力ができたら
保存をクリックしておきます
次に、この入力内容を反映するために
single.php を編集します
<tbody> <tr> <td>○○のお店</td> <td>日曜日</td> <td>11:00〜17:00</td> <td>ランチ1000円</td> <td>東京都</td> <td>000-000-0000</td> <td>http://example.co.jp</td> <td>○○駅○版口から徒歩</td> </tr> </tbody>
の部分を書き換えます
<tbody> <tr> <td><?php echo post_custom("ShopName")?></td> <td><?php echo post_custom("Holiday")?></td> <td><?php echo post_custom("Time")?></td> <td><?php echo post_custom("lunch")?></td> <td><?php echo post_custom("Address")?></td> <td><?php echo post_custom("tel")?></td> <td><?php echo post_custom("Hp")?></td> <td><?php echo post_custom("Access")?></td> </tr> </tbody>
というように書き換えます
<?php echo post_custom("ShopName")?>
というように
post_custom() の引数は
カスタムフィールドで設定したIDになります
ShopName なら店名になります
これでプレビューを見ると
テーブルに内容が反映され表示されます
次に、お店の簡単な紹介文も
カスタムフィールドのものを反映するようにします
<h3>午後の緩やかな時間を本とともに</h3>
を
<?php if ( get_post_meta($post->ID,'ShopName',TRUE) ): ?> <h3><?php echo post_custom("Impression")?></h3> <?php endif;?>
というように修正します
これでほぼ内容を反映できるようになりました
今回のコードは以下のようになります
<?php get_header(); ?> <?php if(have_posts()): while(have_posts()): the_post(); ?> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <?php if ( get_post_meta($post->ID,'ShopName',TRUE) ): ?> <h3><?php echo post_custom("Impression")?></h3> <?php endif;?> <?php the_content(); ?> <img src="image/tee.jpg" alt="" width="300" height="300"> <h3>ギャラリー</h3> <img src="image/food.jpg" alt="" width="300" height="300"> <h3>メニュー</h3> <div data-role="collapsible"> <h3>私のおすすめメニュー</h3> <p>イカスミパスタ</p> </div> <?php if ( get_post_meta($post->ID,'ShopName',TRUE) ): ?> <h3>店舗情報</h3> <table data-role="table" data-mode="reflow" class="ui-responsive"> <thead> <tr> <th>店名</th> <th>定休日</th> <th>営業時間</th> <th>ランチ価格</th> <th>住所</th> <th>電話番号</th> <th>HP</th> <th>アクセス</th> </tr> </thead> <tbody> <tr> <td><?php echo post_custom("ShopName")?></td> <td><?php echo post_custom("Holiday")?></td> <td><?php echo post_custom("Time")?></td> <td><?php echo post_custom("lunch")?></td> <td><?php echo post_custom("Address")?></td> <td><?php echo post_custom("tel")?></td> <td><?php echo post_custom("Hp")?></td> <td><?php echo post_custom("Access")?></td> </tr> </tbody> </table> <h3>周辺地図</h3> <iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d3241.7481415393895!2d139.74543744402948!3d35.65857638920314!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x60188bbd9009ec09%3A0x481a93f0d2a409dd!2z5p2x5Lqs44K_44Ov44O8!5e0!3m2!1sja!2sjp!4v1436313888247" width="600" height="450" frameborder="0" style="border:0" allowfullscreen></iframe> <?php endif;?> <?php endwhile; else: ?> <p>記事はありません</p> <?php endif; ?> <?php get_footer(); ?>