WordPress 技巧:后台文章列表添加排序选项

2023-04-13 13:47:07 浏览数 (1)

下面的代码可以在 WordPress 后台文章列表添加排序选项下拉筛选框,支持多种方式排序。

代码语言:javascript复制
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'=>'升序']
	]);
});

最终效果:

0 人点赞