使用プラグイン Advanced Custom Fields
クイック編集からカスタムフィールドのラジオボタンにチェックを入れて更新するとチェックが取れてしまいます。
個別投稿ページを確認すると情報は反映していました。ラジオボタンにチェックを入れて更新した時にチェックが外れずにする方法わかる方教えてください。
参考サイト https://increment-log.com/quickedit-custom-field/
カスタムフィールド名
publish_type
ラジオボタン選択肢
free : 無
premium : 有
/** * 投稿一覧に項目を追加する */ function my_posts_columns($defaults) { $defaults['publish_type'] = '状況'; return $defaults; } add_filter('manage_posts_columns', 'my_posts_columns'); /** * 投稿一覧に追加した項目に値を表示する */ function my_posts_custom_column($column, $post_id) { switch ($column) { case 'publish_type': $post_meta = get_post_meta($post_id, 'publish_type', true); if ($post_meta == 'free') { echo '無'; } elseif ($post_meta == 'premium') { echo '有'; } else { echo ''; } break; case 'display': } } add_action('manage_posts_custom_column', 'my_posts_custom_column', 10, 2); //クイック編集にカスタムフィールド(状況)の入力フォームを表示 function display_my_custom_quickedit( $publish_name, $post_type ) { static $print_nonce = TRUE; if ($print_nonce) { $print_nonce = FALSE; wp_nonce_field('quick_edit_action', $post_type . '_edit_nonce'); //CSRF対策 } ?> <fieldset class="inline-edit-col-right inline-custom-meta"> <div class="inline-edit-col column-<?php echo $publish_name ?>"> <label class="inline-edit-group"> <?php $publish_type = get_field('publish_type');?> <?php switch ( $publish_name ) { case 'publish_type': ?><span class="title">状況</span><br> <label style="background: #fff;padding: 0px 0 0 10px;border: 1px solid #ddd;"><input type="radio" name="free" value="free"<?php if ($publish_type == 'free'):?>checked<?php endif; ?>>無<br></label> <label style="background: #fff;padding: 0px 0 0 10px;border: 1px solid #ddd;"><input type="radio" name="free" value="premium"<?php if ($publish_type == 'premium'):?>checked<?php endif; ?>>有</label> <?php break; } ?> </label> </div> </fieldset> <?php } add_action('quick_edit_custom_box', 'display_my_custom_quickedit', 10, 2); //カスタムフィールドの保存処理 function save_custom_meta($post_id) { $slug = 'post'; //カスタムフィールドの保存処理をしたい投稿タイプを指定 if ($slug !== get_post_type($post_id)) { return; } if (!current_user_can('edit_post', $post_id)) { return; } $_POST += array("{$slug}_edit_nonce" => ''); if (!wp_verify_nonce($_POST["{$slug}_edit_nonce"], 'quick_edit_action')) { return; } if (isset($_REQUEST['free'])) { update_post_meta($post_id, 'publish_type', $_REQUEST['free'] ); } if (isset($_REQUEST['premium'])) { update_post_meta($post_id, 'publish_type', $_REQUEST['premium'] ); } } add_action('save_post', 'save_custom_meta'); add_action('save_post', 'save_custom_meta'); //管理ページ(投稿一覧)でスクリプトの読み込み function my_admin_edit_foot() { global $post_type; $slug = 'post'; //他の一覧ページで動作しないように投稿タイプの指定をする if ($post_type == $slug) { echo '<script type="text/javascript" src="', get_stylesheet_directory_uri() . '/admin/js/main.js', '"></script>'; } } add_action('admin_footer-edit.php', 'my_admin_edit_foot');
main.js
//カスタムフィールドのクイック編集動作 (function ($) { var $wp_inline_edit = inlineEditPost.edit; inlineEditPost.edit = function (id) { $wp_inline_edit.apply(this, arguments); var $post_id = 0; if (typeof (id) == 'object') $post_id = parseInt(this.getId(id)); if ($post_id > 0) { var $edit_row = $('#edit-' + $post_id); var $post_row = $('#post-' + $post_id); var $free = $('.column-free', $post_row).html(); $(':input[name="publish_type"]', $edit_row).val($free); var $premium = $('.column-premium', $post_row).html(); $(':input[name="publish_type"]', $edit_row).val($premium); } }; })(jQuery);
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/10/11 04:18 編集
退会済みユーザー
2017/10/11 04:57 編集
退会済みユーザー
2017/10/11 07:02
退会済みユーザー
2017/10/11 07:22
退会済みユーザー
2017/10/11 07:43
退会済みユーザー
2017/10/11 08:25
退会済みユーザー
2017/10/11 08:39
退会済みユーザー
2017/10/11 08:51
退会済みユーザー
2017/10/11 08:53
退会済みユーザー
2017/10/11 08:57
退会済みユーザー
2017/10/11 09:03
退会済みユーザー
2017/10/11 09:26
退会済みユーザー
2017/10/11 09:53