我正在编写一个插件,将一组自定义字段添加到“编辑页面”管理屏幕,使用Custom Meta Boxes 2 plugin.这就是我目前写的:
<?php
/**
* Plugin Name: foo Page Banner
* Plugin URI: http://www.foo.eu
* Description: foo custom widget to display a media element banner with custom text, title and links
* Text Domain: foo
* Domain Path: /languages
* Version: 0.0.1
*
* @package foo_Page_Banner
* @version 0.0.1
*/
if ( ! defined( \'ABSPATH\' ) ) {
exit;
}
add_action( \'cmb2_init\', \'add_foo_page_cmb2_metaboxes\' );
function add_foo_page_cmb2_metaboxes() {
$prefix = \'_foo_page_\';
$cmb = new_cmb2_box( array(
\'id\' => $prefix . \'banner_media\',
\'title\' => __( \'Banner media\', \'foo\' ),
\'object_types\' => array( \'page\' ),
\'context\' => \'advanced\',
\'priority\' => \'default\',
) );
$cmb->add_field( array(
\'name\' => __( \'Banner Title\', \'foo\' ),
\'id\' => $prefix . \'banner_title\',
\'type\' => \'text_small\',
\'default\' => \'page title\',
\'desc\' => __( \'Banner Title\', \'foo\' ),
) );
$cmb->add_field( array(
\'name\' => __( \'Banner media\', \'foo\' ),
\'id\' => $prefix . \'banner_media\',
\'type\' => \'file\',
) );
$cmb->add_field( array(
\'name\' => __( \'Banner Description\', \'foo\' ),
\'id\' => $prefix . \'banner_description\',
\'type\' => \'text_medium\',
\'default\' => \'page description\',
) );
$cmb->add_field( array(
\'name\' => __( \'Banner Links\', \'foo\' ),
\'id\' => $prefix . \'banner_links\',
\'type\' => \'text_url\',
\'default\' => \'http://www.example.com\',
\'repeatable\' => true,
) );
}
我感觉需要添加特定操作来加载
add_foo_page_cmb2_metaboxes
仅当当前管理屏幕是页面编辑屏幕时才起作用。我错了吗?我如何做到这一点?
最合适的回答,由SO网友:C Sabhar 整理而成
在代码中,您将“页面”作为对象类型传递。
请按以下方式更新您的代码:
$cmb = new_cmb2_box( array(
\'id\' => $prefix . \'banner_media\',
\'title\' => __( \'Banner media\', \'foo\' ),
\'object_types\' => array( \'post\' , \'page\' ),
\'context\' => \'advanced\',
\'priority\' => \'default\',
) );
将对象类型设置为Post和Page both时,这两种Post类型都可以使用。您还可以包括任何自定义帖子类型。