结合管理_ {$ post_type} _posts_columns过滤器,您可以将自定义列添加或移除(取消设置)到列表文章/页面/自定义文章类型页面(自动出现在屏幕选项中)。这里描述的动作既适用于内置帖子类型也适用于自定义帖子类型。管理_ {$ post_type} _posts_custom_column可以在WP 3.1及更高版本中用于特定的自定义帖子类型。请注意,如果自定义帖子类型的’hierarchical’=> true,那么使用正确的操作挂钩为manage_pages_custom_column。
manage_posts_custom_column预定义的列名称
以下列过滤器已被WordPress定义和使用。这些可以在自定义过滤器切换语句中重新定义。[ 见示例 ] ‘cb’ – 复选框用于选择批量操作的发布项目
‘title’ – 显示帖子标题以及根据用户权限发布操作链接(编辑,快速编辑,垃圾,视图)
‘author’ – 显示发布作者的用户名作为按作者过滤帖子的链接
‘categories’ – 将帖子类别显示为按类别过滤帖子的链接
‘tags’ – 将帖子标签显示为按标签筛选帖子的链接
‘comments’ – 显示评论图标,其中评论数量作为固定链接来管理该帖子的评论
‘date’ – 显示帖子的日期和状态
manage_posts_custom_column参数
已注册的动作函数传递了以下参数。
$column_name
(string) (必需)要显示的列的名称。
默认值:无
$post_id
(int) (必需)当前帖子的ID。也可以从全球$ post-> ID中获取。
默认值:无
manage_posts_custom_column例子
自定义帖子类型
假设您有“books”自定义帖子类型,并且您希望发布日期和书籍作者显示在浏览页面中。
add_action( 'manage_posts_custom_column' , 'custom_columns', 10, 2 );
function custom_columns( $column, $post_id ) {
switch ( $column ) {
case 'book_author':
$terms = get_the_term_list( $post_id, 'book_author', '', ',', '' );
if ( is_string( $terms ) ) {
echo $terms;
} else {
_e( 'Unable to get author(s)', 'your_text_domain' );
}
break;
case 'publisher':
echo get_post_meta( $post_id, 'publisher', true );
break;
}
}
置顶的职位
内置的帖子可以变得置顶。manage_posts_custom_column可以用来在帖子列表中显示。
/* Display custom column */
function display_posts_stickiness( $column, $post_id ) {
if ($column == 'sticky'){
echo '<input type="checkbox" disabled', ( is_sticky( $post_id ) ? ' checked' : ''), '/>';
}
}
add_action( 'manage_posts_custom_column' , 'display_posts_stickiness', 10, 2 );
/* Add custom column to post list */
function add_sticky_column( $columns ) {
return array_merge( $columns,
array( 'sticky' => __( 'Sticky', 'your_text_domain' ) ) );
}
add_filter( 'manage_posts_columns' , 'add_sticky_column' );