前提・実現したいこと
管理画面左「投稿」ボタンの下に「新着情報」を入力できるカスタムフィールドを作成しました。
その内容をfront-page.phpの該当箇所に出力したいです。
プラグインは使わずfunctions.phpに書いてます。
トップページに日付と1行程度の内容を表示したいです。
簡易な新着情報欄です。
発生している問題・エラーメッセージ
front-page.phpへ出力する記述方法がよくわかりません。
###functions.phpの内容
function new_post() { register_post_type( 'news', array( 'label' => 'ニュース登録', 'labels' => array( 'name' => __('ニュース一覧'), 'singular_name' => __('ニュース登録'), 'add_new' => __('ニュース登録'), 'add_new_item' => __('ニュース登録'), 'edit_item' => __('ニュース編集'), 'new_item' => __('新しいニュース'), 'view_item' => __('ニュースを見る'), 'search_items' => __('ニュースを探す'), 'not_found' => __('ニュースはありません'), 'not_found_in_trash' => __('ゴミ箱にニュースはありません'), 'parent_item_colon' => '' ), 'public' => true, 'exclude_from_search' => false, 'show_ui' => true, 'show_in_menu' => true, 'menu_position' => 5, 'hierarchical' => true, 'has_archive' => true, 'supports' => array( 'title', 'thumbnail', 'post-formats', 'page-attributes', 'revisions' ), ) ); } add_action( 'init', 'new_post', 1 ); add_action( 'admin_menu', 'add_custom_field' ); function add_custom_field() { add_meta_box( 'custom-date', '日付', 'item_date', 'news', 'normal' ); add_meta_box( 'custom-content', '内容', 'item_content', 'news', 'normal' ); } function item_date() { $keyname = '日付'; global $post; $get_value = get_post_meta( $post->ID, $keyname, true ); wp_nonce_field( 'action-' . $keyname, 'nonce-' . $keyname ); echo '<input type="date" name="' . $keyname . '" value="' . $get_value . '" size="40" >'; } function item_content(){ $keyname ='内容'; global $post; $get_value = get_post_meta ( $post->ID, $keyname, true); wp_nonce_field('action-' .$keyname, 'nonce-' .$keyname); echo '<input type="text" name="' . $keyname . '" value="' . $get_value . '" size="80" >'; } // カスタムフィールドの保存 function save_custom_field( $post_id ) { if(!empty($_POST['日付'])){ update_post_meta($post_id, '日付', $_POST['日付'] ); }else{ delete_post_meta($post_id, '日付'); } if(!empty($_POST['内容'])){ update_post_meta($post_id, '内容', $_POST['内容'] ); }else{ delete_post_meta($post_id, '内容'); } } add_action('save_post', 'save_custom_field');
front-page.phpの該当箇所
<?php $news = get_field("news"); echo get_post_meta($post->ID , 'custom-date' , true); echo get_post_meta($post->ID , 'custom-content' , true); ?>
さっぱりわからなくて困ってますm(_ _)m
回答1件
あなたの回答
tips
プレビュー