如何更改管理员的发帖顺序?
2 个回答
- 投票数
-
- 2012-09-27
如果您不希望总是单击"标题"(Title)列以标题对帖子进行排序,则可以将此代码放置在当前活动的WordPress主题的
functions.php
文件中或一个插件.这样会始终自动为您排序帖子,因此您不必每次都单击标题栏.您可以使用它来设置帖子类型的默认排序顺序.
/* Sort posts in wp_list_table by column in ascending or descending order. */ function custom_post_order($query){ /* Set post types. _builtin => true returns WordPress default post types. _builtin => false returns custom registered post types. */ $post_types = get_post_types(array('_builtin' => true), 'names'); /* The current post type. */ $post_type = $query->get('post_type'); /* Check post types. */ if(in_array($post_type, $post_types)){ /* Post Column: e.g. title */ if($query->get('orderby') == ''){ $query->set('orderby', 'title'); } /* Post Order: ASC / DESC */ if($query->get('order') == ''){ $query->set('order', 'ASC'); } } } if(is_admin()){ add_action('pre_get_posts', 'custom_post_order'); }
您可以使用其中一些示例条件...
/* Effects all post types in the array. */ if(in_array($post_type, $post_types)){ } /* Effects only a specific post type in the array of post types. */ if(in_array($post_type, $post_types) && $post_type == 'your_post_type_name'){ } /* Effects all post types in the array of post types, except a specific post type. */ if(in_array($post_type, $post_types) && $post_type != 'your_post_type_name'){ }
如果您想对所有帖子类型应用此排序,无论它们是否是"内置" ...
更改此:
$post_types = get_post_types(array('_builtin' => true), 'names');
对此:
$post_types = get_post_types('', 'names');
If you don't wish to always click the "Title" column to sort your posts by title, you can place this code in either your currently active WordPress theme's
functions.php
file, or within a plugin. This will automatically always sort your posts for you, so you don't have to click the title column every time.You can use this for setting default sort order on post types.
/* Sort posts in wp_list_table by column in ascending or descending order. */ function custom_post_order($query){ /* Set post types. _builtin => true returns WordPress default post types. _builtin => false returns custom registered post types. */ $post_types = get_post_types(array('_builtin' => true), 'names'); /* The current post type. */ $post_type = $query->get('post_type'); /* Check post types. */ if(in_array($post_type, $post_types)){ /* Post Column: e.g. title */ if($query->get('orderby') == ''){ $query->set('orderby', 'title'); } /* Post Order: ASC / DESC */ if($query->get('order') == ''){ $query->set('order', 'ASC'); } } } if(is_admin()){ add_action('pre_get_posts', 'custom_post_order'); }
You can use some of these example conditions...
/* Effects all post types in the array. */ if(in_array($post_type, $post_types)){ } /* Effects only a specific post type in the array of post types. */ if(in_array($post_type, $post_types) && $post_type == 'your_post_type_name'){ } /* Effects all post types in the array of post types, except a specific post type. */ if(in_array($post_type, $post_types) && $post_type != 'your_post_type_name'){ }
If you wanted to apply this sorting on ALL post types, regardless of whether or not they are "built-in"...
Change this:
$post_types = get_post_types(array('_builtin' => true), 'names');
To this:
$post_types = get_post_types('', 'names');
-
是否可以在函数中使用而不是在操作`if(!is_admin){ 返回; }`Is it ok to use within the function rather than check before the action `if ( ! is_admin ) { return; }`
- 0
- 2012-09-30
- urok93
-
我想你可以做到.I suppose you could do that.
- 0
- 2012-10-01
- Michael Ecklund
-
您必须添加"返回$ query;".在功能结束之前,否则在更高的wordpress版本中将无法使用.You must add a "return $query;" before function end, otherwise this will not work in later wordpress editions.
- 0
- 2017-04-20
- Jobst
-
我认为插件正在运行此功能并覆盖我的自定义功能.是否有钩子可以确保我的代码而不是插件运行?I think a plugin is running this function and overriding my custom function. Is there a hook to ensure that my code is run rather than the plugins?
- 0
- 2018-08-20
- Thomas_Hoadley
-
- 2012-09-27
啊,点击那个小标题可以切换字母排序....
Ah, click that little title thingy to toggle alphabetical sorting....
如何更改管理控制台中帖子的顺序,以便它们按标题而不是最新的字母顺序显示?