我无法显示自定义帖子类型的列值。我根据 WordPress 文档使用正确的过滤器和操作。
为了确保我做的一切都是正确的,我根据官方文档中的示例准备了一个简单的插件来启动自定义帖子类型并添加自定义列。
不幸的是,我添加的列仍然没有填充“lorem ipsum”。
分析Wordpress代码,我注意到
column_default
中定义的方法wp-admin/includes/class-wp-posts-list-table.php
定义的动作 manage_{$post->post_type}_posts_custom_column
不会被执行。
这是完整的插件代码。
xpost
xpostcol
<?php
/**
* Plugin Name: XPlugin
*/
add_action('init', 'xplugin_register_post_type');
add_filter('manage_xpost_posts_columns', 'xplugin_manage_posts_columns');
add_action('manage_xpost_posts_custom_column', 'xplugin_manage_posts_custom_column', 10, 2 );
register_activation_hook(__FILE__, 'xplugin_register_activation_hook');
function xplugin_register_post_type() {
$labels = array(
'name'=> 'XPosts'
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'xpost' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
);
register_post_type( 'xpost', $args );
}
function xplugin_register_activation_hook() {
xplugin_register_post_type();
flush_rewrite_rules();
}
function xplugin_manage_posts_columns($columns) {
unset($columns['author']);
$new_columns = array('xpostcol' => 'XPost Column');
return array_merge($columns, $new_columns);
}
function xplugin_manage_posts_custom_column($column, $post_id) {
switch ($column) {
case 'xpostcol' :
echo 'Lorem ipsum';
break;
}
}
当我从官方 Docker 映像运行 WordPress 容器时,插件无法正常工作。如果运行基于 Apache + PHP + MySQL 的自定义容器并手动安装 WordPress,该插件可以正常工作。