5ucms论坛

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 16753|回复: 0
打印 上一主题 下一主题

[教程] 向快速编辑区域添加修改排序的功能

[复制链接]

670

主题

785

帖子

8238

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
8238
跳转到指定楼层
楼主
发表于 2021-6-16 14:59:47 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. //快速编辑项增加内容
  2. add_action('quick_edit_custom_box',  'io_add_quick_edit', 10, 2);
  3. function io_add_quick_edit($column_name, $post_type) {
  4.     if ($column_name == 'ranks') {//值与前方代码对应
  5.         //请注意:<fieldset>类可以是:
  6.         //inline-edit-col-left,inline-edit-col-center,inline-edit-col-right
  7.         //所有列均为float:left,
  8.         //因此,如果要在左列,请使用clear:both元素
  9.         ?>
  10.         <fieldset class="inline-edit-col-right">
  11.             <div class="inline-edit-col">
  12.                 <label class="alignleft">
  13.                     <span class="title">排序</span>
  14.                     <span class="input-text-wrap"><input type="number" name="rank" class="ptitle" value="" /></span>
  15.                 </label>
  16.                 <em class="alignleft inline-edit-or"> -999~999 越大越靠前</em>
  17.             </div>
  18.         </fieldset>
  19.         <?
  20.     }
  21. }
  22. //快速编辑处的值填入默认值
  23. add_action('admin_footer', 'ashuwp_quick_edit_javascript');
  24. function ashuwp_quick_edit_javascript() {
  25.     global $current_screen;
  26.         if (($current_screen->id != 'edit-post') || ($current_screen->post_type != 'post')) return;
  27.     //if($current_screen->id == 'edit-sites'){
  28.     //修改下方 js 代码中的 ordinal 为前方代码对应的值
  29.     ?>
  30.         <script type='text/javascript'>
  31.         jQuery(function($){
  32.             var wp_inline_edit_function = inlineEditPost.edit;
  33.             inlineEditPost.edit = function( post_id ) {
  34.                 wp_inline_edit_function.apply( this, arguments );
  35.                 var id = 0;
  36.                 if ( typeof( post_id ) == 'object' ) {
  37.                     id = parseInt( this.getId( post_id ) );
  38.                 }
  39.                 if ( id > 0 ) {
  40.                     var specific_post_edit_row = $( '#edit-' + id ),
  41.                         specific_post_row = $( '#post-' + id ),
  42.                         product_price = $( '.column-ranks', specific_post_row ).text();
  43.                     $('input[name=\"rank\"]', specific_post_edit_row ).val( product_price );
  44.                 }
  45.             }
  46.         });
  47.         </script>
  48.     <?
  49.     //}
  50. }
  51. //保存这个字段
  52. add_action('save_post', 'io_save_quick_edit_data');
  53. function io_save_quick_edit_data($post_id) {
  54.     //如果是自动保存日志,并非我们所提交数据,那就不处理
  55.     if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
  56.         return $post_id;
  57.     // 验证权限,'sites' 为文章类型,默认为 'post' ,这里为我自定义的文章类型'sites'
  58.     if ( 'sites' == $_POST['post_type'] ) {
  59.         if ( !current_user_can( 'edit_page', $post_id ) )
  60.             return $post_id;
  61.     } else {
  62.         if ( !current_user_can( 'edit_post', $post_id ) )
  63.         return $post_id;
  64.     }  
  65.     $post = get_post($post_id);
  66.     // 'ordinal' 与前方代码对应
  67.     if (isset($_POST['rank']) && ($post->post_type != 'rank')) {
  68.         $left_menu_id = esc_attr($_POST['rank']);
  69.         if ($left_menu_id)
  70.             update_post_meta( $post_id, 'rank', $left_menu_id);// ‘_sites_order’为自定义字段
  71.     }
  72. }
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|5ucms.com

GMT+8, 2024-4-27 11:53 , Processed in 0.890625 second(s), 29 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表