manage_pages_custom_column
云策文档标注
概述
manage_pages_custom_column 是一个 WordPress 动作钩子,用于在页面列表表格的自定义列中触发,允许开发者输出自定义列内容。此钩子仅对分层文章类型(如页面)生效。
关键要点
- 钩子名称:manage_pages_custom_column,参数为 $column_name(列名)和 $post_id(文章 ID)。
- 需与 manage_pages_columns 过滤器配合使用,以添加或移除自定义列。
- 对于自定义文章类型,若其具有 'hierarchical' => true 属性,应使用此钩子而非 manage_{$post_type}_posts_custom_column。
- 适用于 WordPress 2.5.0 及以上版本。
代码示例
// 添加“模板”列到页面列表
add_filter( 'manage_pages_columns', 'page_column_views' );
add_action( 'manage_pages_custom_column', 'page_custom_column_views', 5, 2 );
function page_column_views( $defaults ) {
$defaults['page-layout'] = __('Template', 'textdomain');
return $defaults;
}
function page_custom_column_views( $column_name, $id ) {
if ( $column_name === 'page-layout' ) {
$set_template = get_post_meta( get_the_ID(), '_wp_page_template', true );
if ( $set_template == 'default' ) {
echo __('Default Template', 'textdomain');
}
$templates = get_page_templates();
ksort( $templates );
foreach ( array_keys( $templates ) as $template ) :
if ( $set_template == $templates[$template] ) echo $template;
endforeach;
}
}注意事项
- 确保当前文章类型为分层类型(如页面),否则钩子不会触发。
- 在自定义文章类型中,根据 hierarchical 属性选择正确的钩子。
原文内容
Fires in each custom column on the Posts list table.
Description
This hook only fires if the current post type is hierarchical, such as pages.
Parameters
$column_namestring-
The name of the column to display.
$post_idint-
The current post ID.
Source
do_action( 'manage_pages_custom_column', $column_name, $post->ID );
Changelog
| Version | Description |
|---|---|
| 2.5.0 | Introduced. |
Skip to note 3 content
Khoi Pro
To add “Template” column into page list:
add_filter( 'manage_pages_columns', 'page_column_views' ); add_action( 'manage_pages_custom_column', 'page_custom_column_views', 5, 2 ); function page_column_views( $defaults ) { $defaults['page-layout'] = __('Template', 'textdomain'); return $defaults; } function page_custom_column_views( $column_name, $id ) { if ( $column_name === 'page-layout' ) { $set_template = get_post_meta( get_the_ID(), '_wp_page_template', true ); if ( $set_template == 'default' ) { echo __('Default Template', 'textdomain'); } $templates = get_page_templates(); ksort( $templates ); foreach ( array_keys( $templates ) as $template ) : if ( $set_template == $templates[$template] ) echo $template; endforeach; } }To add to other post types (not post, page or attachment…):
The hooks to create custom columns and their associated data for a custom post type are
manage_{$post_type}_posts_columnsandmanage_{$post_type}_posts_custom_columnrespectively, where{$post_type}is the name of the custom post type.// Add the custom columns to the book post type: add_filter( 'manage_book_posts_columns', 'set_custom_edit_book_columns' ); function set_custom_edit_book_columns($columns) { unset( $columns['author'] ); $columns['book_author'] = __( 'Author', 'textdomain' ); $columns['publisher'] = __( 'Publisher', 'textdomain' ); return $columns; } // Add the data to the custom columns for the book post type: add_action( 'manage_book_posts_custom_column' , 'custom_book_column', 10, 2 ); function custom_book_column( $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)', 'textdomain' ); break; case 'publisher' : echo get_post_meta( $post_id , 'publisher' , true ); break; } }Skip to note 4 content
Steven Lin
Example migrated from Codex:
Once you have added your column (for this example, thumbnail), the featured image can be displayed for that page in the new thumbnail column.
function custom_page_column_content( $column_name, $post_id ) { if ( $column_name == 'thumbnail' ) { $post_thumbnail_id = get_post_thumbnail_id( $post_id ); if ( $post_thumbnail_id ) { $post_thumbnail_img = wp_get_attachment_image_src( $post_thumbnail_id, 'thumbnail' ); echo '<img src="' . $post_thumbnail_img[0] . '" />'; } } } add_action( 'manage_pages_custom_column', 'custom_page_column_content', 10, 2 );