LoginSignup
1
3

More than 5 years have passed since last update.

ACF の Date Picker で範囲を指定する。

Last updated at Posted at 2018-01-18

WordPress を使ってカスタムフィールド製造業をしていると、ACF で Date Picker を使う時があります。

スクリーンショット 2018-01-18 16.13.53.png

こんな感じで、どこまでも選択し放題な感じなので、範囲を決めたいと思った時に管理画面から出来ないので
管理画面に以下のような js を読み込めば大丈夫だと思います。

example.js
(function($){
    var post      = $('#post');
    var postType  = $('#post_type').val();
    var postTypes = [ 'post', 'page' ];

    if ( -1 !== $.inArray( postType, postTypes ) ) {
        var datePickerField = $('#acf-xxxxxx'); // xxxxxx の部分は編集画面の表示オプションに Field Keys があるので、チェックして確認
        datePickerField.next('input').datepicker({ minDate: -1, maxDate: "+1d" }); // とりあえず前後一日を指定
    }

})(jQuery);

ファイルの読み込みは割愛します。

スクリーンショット 2018-01-18 16.14.24.png

うまいこと読み込めれば、上記画像みたいになります。
すでに出てたり知っていたらすみません。

追記:
date_picker_args があるので、さっきの example.js は

example.js
(function($){
acf.add_filter('date_picker_args', function( args, $field ){
    if ( 'field_xxxx' === $field.data('key') ) { // field_xxxx の部分は編集画面の表示オプションに Field Keys があるので、チェックして確認
        args.minDate = '+1d';
        args.maxDate = '+3m';
    }
    return args;
});
})(jQuery);

上記で大丈夫みたいでした

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3