alter table add column多个字段_WordPress 在文章列表快速编辑中编辑自定义字段
在快速編輯中添加自定義字段,通常我們不需要在 WordPress 后臺文章列表的“快速編輯”菜單中進行更改,但有的時候如果可以把常用的設置添加到“快速編輯”菜單里,可以節省您很多時間。如下方 gif 的操作,在列表快速編輯排序。
添加列
這里以 WebStack 導航主題的自定義文章類型“sites”(網址)為例,注意修改下面代碼的sites字眼為自己的。
一為:WordPress導航主題-WebStack導航主題?zhuanlan.zhihu.com這里添加兩個選項-排序和可見性選項。下面的代碼是針對sites自定義類型的,記得修改。
/*** 文章列表添加自定義字段* https://www.iowen.cn/wordpress-quick-edit*/ //如果是默認文章,請使用 manage_post_posts_columns 鉤子,這里是自定義文章類型 sites add_filter('manage_edit-sites_columns', 'io_ordinal_manage_posts_columns'); function io_ordinal_manage_posts_columns($columns){$columns['ordinal'] = '排序'; $columns['visible'] = '可見性'; return $columns; } add_action('manage_posts_custom_column','io_ordinal_manage_posts_custom_column',10,2); function io_ordinal_manage_posts_custom_column($column_name,$id){ switch( $column_name ) :case 'ordinal': {echo get_post_meta($id, '_sites_order', true);break;}case 'visible': {echo get_post_meta($id, '_visible', true)? "管理員" : "所有人";break;}endswitch; }添加了列之后,你也可以使用CSS來使其中的一些列變得更寬或更窄。如下示例
add_action( 'admin_head', 'io_custom_css' ); function io_custom_css(){echo '<style>#ordinal{/*名稱與上方代碼對應*/width:80px;} </style>'; }2. 添加快速編輯表單
使用quick_edit_custom_box鉤子。
add_action('quick_edit_custom_box', 'io_add_quick_edit', 10, 2); function io_add_quick_edit($column_name, $post_type) {if ($column_name == 'ordinal') {//值與前方代碼對應//請注意:<fieldset>類可以是://inline-edit-col-left,inline-edit-col-center,inline-edit-col-right//所有列均為float:left,//因此,如果要在左列,請使用clear:both元素echo '<fieldset class="inline-edit-col-left" style="clear: both;"><div class="inline-edit-col"> <label class="alignleft"><span class="title">排序</span><span class="input-text-wrap"><input type="number" name="ordinal" class="ptitle" value=""></span></label> <em class="alignleft inline-edit-or"> 越大越靠前</em></div></fieldset>';} }完成此步驟后,字段應出現在“快速編輯”中。不用擔心這些字段的值是否為空,我們將在最后一步中填充它們。
3. 保存字段
/** * 文章列表添加自定義字段 * https://www.iowen.cn/wordpress-quick-edit*/ add_action('save_post', 'io_save_quick_edit_data'); function io_save_quick_edit_data($post_id) {//如果是自動保存日志,并非我們所提交數據,那就不處理if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )return $post_id;// 驗證權限,'sites' 為文章類型,默認為 'post' ,這里為我自定義的文章類型'sites'if ( 'sites' == $_POST['post_type'] ) {if ( !current_user_can( 'edit_page', $post_id ) )return $post_id;} else {if ( !current_user_can( 'edit_post', $post_id ) )return $post_id;} $post = get_post($post_id); // 'ordinal' 與前方代碼對應if (isset($_POST['ordinal']) && ($post->post_type != 'revision')) {$left_menu_id = esc_attr($_POST['ordinal']);if ($left_menu_id)update_post_meta( $post_id, '_sites_order', $left_menu_id);// ‘_sites_order’為自定義字段} }完成此步驟后,單擊“更新”按鈕,應在自定義列中已經更新了“快速編輯”字段。
4. 加載默認數據
因為第2步添加的表單不能獲取文章的默認值,為了編輯方便,所有需要將默認值賦予表單
/*** 文章列表添加自定義字段* https://www.iowen.cn/wordpress-quick-edit*/ add_action('admin_footer', 'ashuwp_quick_edit_javascript'); function ashuwp_quick_edit_javascript() {$current_screen = get_current_screen(); //條件判斷,注意修改為對應值if (!is_object($current_screen) || ($current_screen->post_type != 'sites'))return;if($current_screen->id == 'edit-sites'){//修改下方 js 代碼中的 ordinal 為前方代碼對應的值echo"<script type='text/javascript'>jQuery(function($){var wp_inline_edit_function = inlineEditPost.edit;inlineEditPost.edit = function( post_id ) {wp_inline_edit_function.apply( this, arguments );var id = 0;if ( typeof( post_id ) == 'object' ) {id = parseInt( this.getId( post_id ) );}if ( id > 0 ) {var specific_post_edit_row = $( '#edit-' + id ),specific_post_row = $( '#post-' + id ),product_price = $( '.column-ordinal', specific_post_row ).text(); $('input[name="ordinal"]', specific_post_edit_row ).val( product_price ); }}});</script>";} }好了,現在可以快速編輯排序了,還可以為列表添加分類過濾器,選擇分類單獨調整分類下網址的排序。添加方法下面繼續
5. 添加分類過濾器
/*** 文章列表添加自定義字段* https://www.iowen.cn/wordpress-quick-edit*/ //此部分功能是生成分類下拉菜單 add_action('restrict_manage_posts','io_post_type_filter',10,2); function io_post_type_filter($post_type, $which){if('sites' !== $post_type){ //這里為自定義文章類型,需修改return; //檢查是否是我們需要的文章類型}$taxonomy_slug = 'favorites'; //這里為自定義分類法,需修改$taxonomy = get_taxonomy($taxonomy_slug);$selected = '';$request_attr = 'favorites'; //這里為自定義分類法,需修改if ( isset($_REQUEST[$request_attr] ) ) {$selected = $_REQUEST[$request_attr];}wp_dropdown_categories(array('show_option_all' => __("所有{$taxonomy->label}"),'taxonomy' => $taxonomy_slug,'name' => $request_attr,'orderby' => 'name','selected' => $selected,'hierarchical' => true,'depth' => 5,'show_count' => true, 'hide_empty' => false, )); } //此部分功能是列出指定分類下的所有文章 add_filter('parse_query','io_work_convert_restrict'); function io_work_convert_restrict($query) { global $pagenow; global $typenow; if ($pagenow=='edit.php') { $filters = get_object_taxonomies($typenow); foreach ($filters as $tax_slug) { $var = &$query->query_vars[$tax_slug]; if ( isset($var) && $var>0) { $term = get_term_by('id',$var,$tax_slug); $var = $term->slug; } } } return $query; }分類列表
分類列表也是可以這樣編輯的,使用manage_edit-分類法名_columns和manage_分類法名_custom_column這兩個鉤子完成第1步,第2步和上方相同,第3步使用add_action('edit_分類法名','function',10,1);鉤子,第4步js代碼中的inlineEditPost改為inlineEditTax和一些標簽的修改,就可以實現分類列表的快速編輯了。
總結
以上是生活随笔為你收集整理的alter table add column多个字段_WordPress 在文章列表快速编辑中编辑自定义字段的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何升级浏览器_绿茶浏览器app下载安装
- 下一篇: mysql 去重_点赞功能,用 MySQ