サポート » 使い方全般 » カスタムフィールドをスケジュール表示に使用したい

  • 解決済 unjmj

    (@unjmj)


    カスタムフィールドを利用して「今日から3日間の予定」などのスケジュールを管理したいのですが、
    日付を自動的にキーにセットし、値(前日までに入力したスケジュール)を
    以下のように動的に処理する方法はないでしょうか?

    例:
    今日「1月14日」のカスタムフィールド

    キー(日付) 値(予定)
    1月14日】【”スケジュールA“】
    1月15日】【”スケジュールB“】
    1月16日】【”スケジュールC“】
    ※値(予定)のみユーザーが入力

    ———————————–

    翌日「1月15日」のカスタムフィールド

    キー(日付) 値(予定)
    1月15日】【”スケジュールB“】←前日のキーと値は消え、入力していた値が移動
    1月16日】【”スケジュールC“】← 〃
    1月17日】【  ”未定”  】←未入力のため「未定」の表示

    PHP駆け出しの為漠然とした提示しか出来ずすみません。
    特定のページのカスタムフィールドに、上記のような処理を実装するのは難しいことでしょうか?
    標準のカスタムフィールドの機能・用途からは懸け離れているので・・・

    他に適切な手段があればそちらもご教示頂ければと思います。
    宜しくお願いします。

3件の返信を表示中 - 1 - 3件目 (全3件中)
  • meta box を追加してみました。
    キーの値は「date-01-14」の形式です。

    function my_meta_schedule_box(){
      add_meta_box('my_meta_schedule_box', '今日から3日間の予定', 'my_meta_schedule_html', 'post', 'normal', 'high');
    }
    function my_meta_schedule_html($post, $box){
      echo '<input type="hidden" name="my_meta_schedule_nonce" id="my_meta_schedule_nonce" value="'.wp_create_nonce(get_bloginfo('template_url')).'" />';
      for($i=0; $i<3; $i++){
    	$time = strtotime('+' . $i . ' days');
    	$key = 'date-' . date('m-d', $time);
    	echo '<div><label for="' . $key . '">'. date('n月j日', $time) .'</label>';
    	echo '<input type="text" name="' . $key . '" value="'. get_post_meta($post->ID, $key, true) .'" size="50" /></div>';
      }
    }
    function my_meta_schedule_update($post_id){
      if (!wp_verify_nonce( $_POST['my_meta_schedule_nonce'], get_bloginfo('template_url'))){
        return $post_id;
      }
      if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
        return $post_id;
    
      if(!current_user_can('edit_post', $post_id))
        return $post_id;
    
      $key = date('m-d', strtotime('-1 day'));
      delete_post_meta($post_id, $key);
    
      for($i=0; $i<3; $i++){
    	$time  = strtotime('+' . $i . ' days');
    	$key = 'date-' . date('m-d', $time);
    	$value = $_POST[$key];
        if($value == '') $value = '未定';
    	update_post_meta($post_id, $key, $value);
      }
    }
    add_action('admin_menu', 'my_meta_schedule_box');
    add_action('save_post', 'my_meta_schedule_update');

    トピック投稿者 unjmj

    (@unjmj)

    kz様
    わざわざ完成形で示してくださり、本当にありがとうございます。
    試してみたところ、まさに希望通りの動作をしているようです。
    半ば諦めていたところなので大変感激しております。

    ご教示頂いた内容について、1つ1つ理解出来るよう順を追って
    じっくりと学んでいきたいと思います。
    本当にありがとうございました。

    古い記事に書き込み失礼します。

    <?php
      for($i=0; $i<7; $i++){
    	$time = strtotime('+' . $i . ' days');
    	$key = 'date-' . date('m-d', $time);
    	echo '<td>'. datedate('n月j日', $time) .'</td>';
      }
    ; ?>

    で記事への出力は可能ですが、曜日を表示するにはどうしたら良いでしょうか?

3件の返信を表示中 - 1 - 3件目 (全3件中)
  • トピック「カスタムフィールドをスケジュール表示に使用したい」には新たに返信することはできません。