WordPress 技巧:后台文章列表添加排序选项
下面的代码可以在 WordPress 后台文章列表添加排序选项下拉筛选框,支持多种方式排序。
add_action('restrict_manage_posts', function($post_type){
global $wp_list_table;
list($columns, $hidden, $sortable_columns, $primary) = $wp_list_table->get_column_info();
foreach($sortable_columns as $sortable_column => $data){
if(isset($columns[$sortable_column])){
$orderby_options[$sortable_column] = $columns[$sortable_column];
}
}
echo wpjam_get_field_html([
'title' =>'',
'key' =>'orderby',
'type' =>'select',
'value' =>$_REQUEST['orderby'] ?? '',
'options' =>$orderby_options
]);
echo wpjam_get_field_html([
'title' =>'',
'key' =>'order',
'type' =>'select',
'value' =>$_REQUEST['order'] ?? 'DESC',
'options' =>['desc'=>'降序','asc'=>'升序']
]);
});
最终效果:
该功能已经整合到 WPJAM Basic 插件中,并已免费提供下载,简单勾选或者设置下即可开启!