我想在我的所有页面(不是帖子)中添加一个元框,但看起来我只能为基于抄本的特定帖子添加元框?
add_meta_box( $id, $title, $callback, $page, $context = \'advanced\', $priority = \'default\', $callback_args = null );
我想在我的所有页面(不是帖子)中添加一个元框,但看起来我只能为基于抄本的特定帖子添加元框?
add_meta_box( $id, $title, $callback, $page, $context = \'advanced\', $priority = \'default\', $callback_args = null );
试试这个。这是我使用的一些代码的一部分。
// Add meta boxes to Page
add_meta_box( \'RelatedImage\', __(\'Related images\'), \'related_images_meta_box\', \'page\', \'normal\', \'high\' );
function related_images_meta_box($object) {
$nonce = wp_create_nonce(plugin_basename( __FILE__ ));
?>
<input type="hidden" name="wp_filebrowser_nonce" id="wp_filebrowser_nonce" value="<?php echo $nonce; ?>" />
<table>
<tr>
<td>
<label for="related_image" class="">Related image</label>
</td>
<td>
<input type="text" id="related_image" name="related_image" value="<?php echo \'your meta property\' ?>" size="40" />
<td>
</tr>
</table>
<?php }
Wordpress默认具有以下帖子类型,您的自定义帖子类型添加在数组的末尾:
Array
(
[0] => post
[1] => page
[2] => attachment
[3] => revision
[4] => nav_menu_item
[5] => ...all your custom post types
)
因此,您可以循环浏览它们,只需为每个帖子类型添加一个元框。注意:这不适用于attachment
, revision
和nav_menu_item
, 所以你想跳过它们:function wpse44962_add_meta_boxes()
{
foreach ( array_keys( $GLOBALS[\'wp_post_types\'] ) as $post_type )
{
// Skip:
if ( in_array( $post_type, array( \'attachment\', \'revision\', \'nav_menu_item\' ) ) )
continue;
// You\'ll have to set $id, $title, $callback yourself:
add_meta_box( $id, $title, $callback, $post_type, \'advanced\', \'default\' );
}
}
add_action( \'add_meta_boxes\', \'wpse44962_add_meta_boxes\' );
我正在处理一个自定义的帖子类型,我删除了所有标准的wordpress表单项,并从零开始使用自定义的元框,除了标题字段。在我的情况下,选择一些自定义元下拉选择非常重要。是否有一个简单的解决方案,让他们在发布项目之前必须选择一些项目?我认为javascript是最简单的解决方案,但最好让用户知道发生了什么,比如如果他们试图发布但尚未选中,则突出显示框,另一个问题是下拉菜单,默认情况下,即使他们没有选择一个值,也已经选择了一个值