我有一个父主题,它创建了许多自定义的元框,这些元框被添加到“post”和“pages”帖子类型中,还有一些由主题创建的帖子类型。
在我的子主题中,我创建了一些新的自定义帖子类型,因此我想在子主题的某处添加一个函数,将这些预先存在的元框添加到子主题中定义的新帖子类型中。
我已经尝试了很多方法,但在每种情况下,除了在父主题中编辑元框文件外,元框都无法添加到我的自定义帖子类型中。
我很想学习如何做到这一点。这里有人能教我怎么做吗?
谢谢
我有一个父主题,它创建了许多自定义的元框,这些元框被添加到“post”和“pages”帖子类型中,还有一些由主题创建的帖子类型。
在我的子主题中,我创建了一些新的自定义帖子类型,因此我想在子主题的某处添加一个函数,将这些预先存在的元框添加到子主题中定义的新帖子类型中。
我已经尝试了很多方法,但在每种情况下,除了在父主题中编辑元框文件外,元框都无法添加到我的自定义帖子类型中。
我很想学习如何做到这一点。这里有人能教我怎么做吗?
谢谢
这取决于父主题在meta\\u框中的挂钩方式。如果回调连接到add_meta_boxes
与法典中的以下内容类似:
function myplugin_add_meta_box() {
$screens = array( \'post\', \'page\' );
foreach ( $screens as $screen ) {
add_meta_box(
\'myplugin_sectionid\',
__( \'My Post Section Title\', \'myplugin_textdomain\' ),
\'myplugin_meta_box_callback\',
$screen
);
}
}
add_action( \'add_meta_boxes\', \'myplugin_add_meta_box\' );
那么,如果不破解文件,就无法添加框。那个$screens = array( \'post\', \'page\' );
接下来的数组将阻止它。同样,您将无法添加:
function adding_custom_meta_boxes( $post_type, $post ) {
if (\'abcd\' == $post_type) return;
add_meta_box(
\'my-meta-box\',
__( \'My Meta Box\' ),
\'render_my_meta_box\',
\'post\',
\'normal\',
\'default\'
);
}
add_action( \'add_meta_boxes\', \'adding_custom_meta_boxes\', 10, 2 );
if (\'abcd\' == $post_type) return;
将阻止它。但是,如果它与立柱类型的特定挂钩连接as recommended...
add_action( \'add_meta_boxes_post\', \'adding_custom_meta_boxes\' );
。。。然后添加其他内容应该很容易:add_action( \'add_meta_boxes_mytype\', \'adding_custom_meta_boxes\' );
add_action( \'add_meta_boxes_myothertype\', \'adding_custom_meta_boxes\' );
如果您可以找到父主题定义并挂钩到的函数add_meta_boxes
, 您可以删除动作并在子主题中重新定义它。
function my_child_theme_meta_box_override_cb() {
$post_types = array( \'post\', \'page\', \'my_other_post_type\' );
// copy the add_meta_box function from the parent theme hook below...
}
function wpse_override_meta_box_action(){
remove_action( \'add_meta_boxes\', \'parent_theme_meta_box_hook_cb\' );
add_action( \'add_meta_boxes\', \'my_child_theme_meta_box_override_cb\' );
}
add_action( \'after_setup_theme\', \'wpse_override_meta_box_action\' );
我使用此代码显示帖子类型下拉列表,以选择可用的帖子类型,但当我提交帖子时,它会将值还原回帖子,如何确保它会保存它?<select name=\'my_meta_box_post_type\' id=\'my_meta_box_post_type\'> <?php $post_types=get_post_types(\'\', \'objects\'); foreach ($post_types as $post_type): ?>