@@ -124,7 +124,12 @@ function term_order_field( $term, $taxonomy ) {
124
124
add_action ('created_favorites ' ,'save_term_order ' ,10 ,1 );
125
125
add_action ('edit_favorites ' ,'save_term_order ' ,10 ,1 );
126
126
function save_term_order ( $ term_id ) {
127
- update_term_meta ( $ term_id , '_term_order ' , $ _POST [ '_term_order ' ] );
127
+ if (isset ($ _POST ['_term_order ' ])) {
128
+ update_term_meta ( $ term_id , '_term_order ' , $ _POST [ '_term_order ' ] );
129
+ }
130
+ $ ca_menu_id = esc_attr ($ _POST ['ca_ordinal ' ]);
131
+ if ($ ca_menu_id )
132
+ update_term_meta ( $ term_id , '_term_order ' , $ ca_menu_id );
128
133
}
129
134
130
135
@@ -145,10 +150,247 @@ function custom_sites_rewrites_init(){
145
150
add_rewrite_rule (
146
151
'sites/([0-9]+)?.html$ ' ,
147
152
'index.php?post_type=sites&p=$matches[1] ' ,
148
- 'top ' );
153
+ 'top '
154
+ );
149
155
add_rewrite_rule (
150
156
'sites/([0-9]+)?.html/comment-page-([0-9]{1,})$ ' ,
151
157
'index.php?post_type=sites&p=$matches[1]&cpage=$matches[2] ' ,
152
158
'top '
153
- );
154
- }
159
+ );
160
+ }
161
+
162
+
163
+ //此部分功能是生成分类下拉菜单
164
+ add_action ('restrict_manage_posts ' ,'io_post_type_filter ' ,10 ,2 );
165
+ function io_post_type_filter ($ post_type , $ which ){
166
+ if ('sites ' !== $ post_type ){ //这里为自定义文章类型,需修改
167
+ return ; //检查是否是我们需要的文章类型
168
+ }
169
+ $ taxonomy_slug = 'favorites ' ; //这里为自定义分类法,需修改
170
+ $ taxonomy = get_taxonomy ($ taxonomy_slug );
171
+ $ selected = '' ;
172
+ $ request_attr = 'favorites ' ; //这里为自定义分类法,需修改
173
+ if ( isset ($ _REQUEST [$ request_attr ] ) ) {
174
+ $ selected = $ _REQUEST [$ request_attr ];
175
+ }
176
+ wp_dropdown_categories (array (
177
+ 'show_option_all ' => __ ("所有 {$ taxonomy ->label }" ),
178
+ 'taxonomy ' => $ taxonomy_slug ,
179
+ 'name ' => $ request_attr ,
180
+ 'orderby ' => 'name ' ,
181
+ 'selected ' => $ selected ,
182
+ 'hierarchical ' => true ,
183
+ 'depth ' => 5 ,
184
+ 'show_count ' => true , // Show number of post in parent term
185
+ 'hide_empty ' => false , // Don't show posts w/o terms
186
+ ));
187
+ }
188
+ //此部分功能是列出指定分类下的所有文章
189
+ add_filter ('parse_query ' ,'io_work_convert_restrict ' );
190
+ function io_work_convert_restrict ($ query ) {
191
+ global $ pagenow ;
192
+ global $ typenow ;
193
+ if ($ pagenow =='edit.php ' ) {
194
+ $ filters = get_object_taxonomies ($ typenow );
195
+ foreach ($ filters as $ tax_slug ) {
196
+ $ var = &$ query ->query_vars [$ tax_slug ];
197
+ if ( isset ($ var ) && $ var >0 ) {
198
+ $ term = get_term_by ('id ' ,$ var ,$ tax_slug );
199
+ $ var = $ term ->slug ;
200
+ }
201
+ }
202
+ }
203
+ return $ query ;
204
+ }
205
+
206
+ /**
207
+ * 文章列表添加自定义字段
208
+ * https://www.iowen.cn/wordpress-quick-edit
209
+ */
210
+ add_filter ('manage_edit-sites_columns ' , 'io_ordinal_manage_posts_columns ' );
211
+ add_action ('manage_posts_custom_column ' ,'io_ordinal_manage_posts_custom_column ' ,10 ,2 );
212
+ function io_ordinal_manage_posts_columns ($ columns ){
213
+ $ columns ['ordinal ' ] = '排序 ' ;
214
+ $ columns ['visible ' ] = '可见性 ' ;
215
+ return $ columns ;
216
+ }
217
+ function io_ordinal_manage_posts_custom_column ($ column_name ,$ id ){
218
+ switch ( $ column_name ) :
219
+ case 'ordinal ' : {
220
+ echo get_post_meta ($ id , '_sites_order ' , true );
221
+ break ;
222
+ }
223
+ case 'visible ' : {
224
+ echo get_post_meta ($ id , '_visible ' , true )? "管理员 " : "所有人 " ;
225
+ break ;
226
+ }
227
+ endswitch ;
228
+ }
229
+
230
+ //分类列表添加自定义字段
231
+ add_filter ('manage_edit-favorites_columns ' , 'io_id_manage_tags_columns ' );
232
+ add_action ('manage_favorites_custom_column ' ,'io_id_manage_tags_custom_column ' ,10 ,3 );
233
+ function io_id_manage_tags_columns ($ columns ){
234
+ $ columns ['ca_ordinal ' ] = '菜单排序 ' ;
235
+ $ columns ['id ' ] = 'ID ' ;
236
+ return $ columns ;
237
+ }
238
+ function io_id_manage_tags_custom_column ($ null ,$ column_name ,$ id ){
239
+ if ($ column_name == 'ca_ordinal ' ) {
240
+ echo get_term_meta ($ id , '_term_order ' , true );
241
+ }
242
+ if ($ column_name == 'id ' ) {
243
+ echo $ id ;
244
+ }
245
+ }
246
+
247
+ /**
248
+ * 文章列表添加自定义字段
249
+ *
250
+ */
251
+ add_action ( 'admin_head ' , 'io_custom_css ' );
252
+ function io_custom_css (){
253
+ echo '<style>
254
+ #ordinal{
255
+ width:80px;
256
+ }
257
+ </style> ' ;
258
+ }
259
+
260
+ //文章列表添加排序规则
261
+ add_filter ('manage_edit-sites_sortable_columns ' , 'sort_sites_order_column ' );
262
+ //add_filter('manage_edit-favorites_sortable_columns', 'sort_favorites_order_column');
263
+ add_action ('pre_get_posts ' , 'sort_sites_order ' );
264
+ function sort_sites_order_column ($ defaults )
265
+ {
266
+ $ defaults ['ordinal ' ] = 'ordinal ' ;
267
+ return $ defaults ;
268
+ }
269
+ function sort_favorites_order_column ($ defaults )
270
+ {
271
+ $ defaults ['ca_ordinal ' ] = 'ca_ordinal ' ;
272
+ return $ defaults ;
273
+ }
274
+ function sort_sites_order ($ query ) {
275
+ if (!is_admin ())
276
+ return ;
277
+ $ orderby = $ query ->get ('orderby ' );
278
+ if ('ordinal ' == $ orderby ) {
279
+ $ query ->set ('meta_key ' , '_sites_order ' );
280
+ $ query ->set ('orderby ' , 'meta_value_num ' );
281
+ }
282
+ if ('ca_ordinal ' == $ orderby ) {
283
+ $ query ->set ('meta_key ' , '_term_order ' );
284
+ $ query ->set ('orderby ' , 'meta_value_num ' );
285
+ }
286
+ }
287
+
288
+
289
+ add_action ('quick_edit_custom_box ' , 'io_add_quick_edit ' , 10 , 2 );
290
+ function io_add_quick_edit ($ column_name , $ post_type ) {
291
+ if ($ column_name == 'ordinal ' ) {
292
+ //请注意:<fieldset>类可以是:
293
+ //inline-edit-col-left,inline-edit-col-center,inline-edit-col-right
294
+ //所有列均为float:left,
295
+ //因此,如果要在左列,请使用clear:both元素
296
+ echo '
297
+ <fieldset class="inline-edit-col-left" style="clear: both;">
298
+ <div class="inline-edit-col">
299
+ <label class="alignleft">
300
+ <span class="title">排序</span>
301
+ <span class="input-text-wrap"><input type="number" name="ordinal" class="ptitle" value=""></span>
302
+ </label>
303
+ <em class="alignleft inline-edit-or"> 越大越靠前</em>
304
+ </div>
305
+ </fieldset> ' ;
306
+ }
307
+ if ($ column_name == 'ca_ordinal ' ) {
308
+ echo '
309
+ <fieldset>
310
+ <div class="inline-edit-col">
311
+ <label class="alignleft">
312
+ <span class="title">排序</span>
313
+ <span class="input-text-wrap"><input type="number" name="ca_ordinal" class="ptitle" value=""></span>
314
+ </label>
315
+ <em class="alignleft inline-edit-or"> 越大越靠前</em>
316
+ </div>
317
+ </fieldset> ' ;
318
+ }
319
+ }
320
+
321
+
322
+ //保存和更新数据
323
+ add_action ('save_post ' , 'io_save_quick_edit_data ' );
324
+ function io_save_quick_edit_data ($ post_id ) {
325
+ //如果是自动保存日志,并非我们所提交数据,那就不处理
326
+ if ( defined ('DOING_AUTOSAVE ' ) && DOING_AUTOSAVE )
327
+ return $ post_id ;
328
+ // 验证权限,'sites' 为文章类型,默认为 'post' ,这里为我自定义的文章类型'sites'
329
+ if ( 'sites ' == $ _POST ['post_type ' ] ) {
330
+ if ( !current_user_can ( 'edit_page ' , $ post_id ) )
331
+ return $ post_id ;
332
+ } else {
333
+ if ( !current_user_can ( 'edit_post ' , $ post_id ) )
334
+ return $ post_id ;
335
+ }
336
+ $ post = get_post ($ post_id );
337
+ // 'ordinal' 与前方代码对应
338
+ if (isset ($ _POST ['ordinal ' ]) && ($ post ->post_type != 'revision ' )) {
339
+ $ left_menu_id = esc_attr ($ _POST ['ordinal ' ]);
340
+ if ($ left_menu_id )
341
+ update_post_meta ( $ post_id , '_sites_order ' , $ left_menu_id );// ‘_sites_order’为自定义字段
342
+ }
343
+ }
344
+
345
+ //输出js
346
+ add_action ('admin_footer ' , 'ashuwp_quick_edit_javascript ' );
347
+ function ashuwp_quick_edit_javascript () {
348
+ $ current_screen = get_current_screen ();
349
+ if (!is_object ($ current_screen ) || ($ current_screen ->post_type != 'sites ' ))return ;
350
+ if ($ current_screen ->id == 'edit-sites ' ){
351
+ echo "
352
+ <script type='text/javascript'>
353
+ jQuery(function($){
354
+ var wp_inline_edit_function = inlineEditPost.edit;
355
+ inlineEditPost.edit = function( post_id ) {
356
+ wp_inline_edit_function.apply( this, arguments );
357
+ var id = 0;
358
+ if ( typeof( post_id ) == 'object' ) {
359
+ id = parseInt( this.getId( post_id ) );
360
+ }
361
+ if ( id > 0 ) {
362
+ var specific_post_edit_row = $( '#edit-' + id ),
363
+ specific_post_row = $( '#post-' + id ),
364
+ product_price = $( '.column-ordinal', specific_post_row ).text();
365
+
366
+ $('input[name= \"ordinal \"]', specific_post_edit_row ).val( product_price );
367
+ }
368
+ }
369
+ });
370
+ </script> " ;
371
+ }
372
+ if ($ current_screen ->id == 'edit-favorites ' ){
373
+ echo "
374
+ <script type='text/javascript'>
375
+ jQuery(function($){
376
+ var wp_inline_edit_function = inlineEditTax.edit;
377
+ inlineEditTax.edit = function( post_id ) {
378
+ wp_inline_edit_function.apply( this, arguments );
379
+ var id = 0;
380
+ if ( typeof( post_id ) == 'object' ) {
381
+ id = parseInt( this.getId( post_id ) );
382
+ }
383
+ console.log('调试区'+id);
384
+ if ( id > 0 ) {
385
+ var specific_post_edit_row = $( '#edit-' + id ),
386
+ specific_post_row = $( '#tag-' + id ),
387
+ product_price = $( '.column-ca_ordinal', specific_post_row ).text();
388
+
389
+ $('input[name= \"ca_ordinal \"]', specific_post_edit_row ).val( product_price );
390
+ }
391
+ }
392
+ });
393
+ </script> " ;
394
+ }
395
+ }
396
+
0 commit comments