edit.php
是帖子/页面list, 后期创建/编辑屏幕是两个单独的文件:post-new.php
和post.php
.
因此,您可以使用"load-$page"
挂钩,例如:
add_action( \'load-post.php\', function() {
// add metabox or whatever you want when EDITING post
add_action( \'add_meta_boxes\', \'myplugin_meta_box_editing\' );
} );
add_action( \'load-post-new.php\', function() {
// add metabox or whatever you want when creating NEW post
add_action( \'add_meta_boxes\', \'myplugin_meta_box_new\' );
} );
获取更具体的信息(例如,正在创建/编辑的职位类型),如下所示
@KrzysiekDróżdż alredy说,你可以使用当前屏幕对象,但是我建议你使用
get_current_screen()
porpose的函数。
add_action( \'load-post.php\', function() {
// add metabox or whatever you want when EDITING post
$screen = get_current_screen();
// this should give you an idea of the infromation you can get
// DO NOT USE IN PRODUCTION
echo \'<pre>\';
print_r( $screen );
die();
} );
当添加元盒时,作为替代或依赖于以前的方法,用于输出元盒的回调(第3个参数
add_metabox
函数)接收post id,因此使用
get_post()
您可以检索正在编辑的post对象
post_status
您可以知道帖子是否刚刚创建:
add_action( \'add_meta_boxes\', \'myplugin_add_meta_box\' );
function myplugin_add_meta_box() {
// do you need information about screen?
// this can be used, e.g. to use different metabox callback if
// editing or creating a new post
// $screen = get_current_screen();
add_meta_box(
\'myplugin_sectionid\', // metabox id
__( \'My Post Section Title\', \'myplugin_textdomain\' ), // metabox label
\'myplugin_meta_box_callback\', // metabox callback, see below
\'post\' // this metabox is for \'post\' post type
);
}
function myplugin_meta_box_callback( $post_id ) {
$post = get_post( $post_id );
$status = $post->post_status;
if ( $status === \'new\' || $status === \'auto-draft\' ) {
// this is a new post
} else {
// editing an already saved post
}
}