下午好
遵循此页面成功地将Metabox字段添加到我的管理页面,现在我正在尝试使其对我有用:https://paulund.co.uk/create-custom-meta-boxes
我正在尝试为我的页面制作一个简单的食谱帖子,我希望能够在一个字段中输入多个成分。。。理想情况下,每行一个。然后将该元数据输出为数组,用于/每次处理,以便在我的页面上创建列表。
我在函数中添加了以下内容。php:
// Lets get meta
function add_embed_recipe_meta_box() {
add_meta_box(
\'embed_recipe_meta_box\', // $id
\'Recipe Post meta box\', // $title
\'show_embed_recipe_meta_box\', // $callback
\'post\', // $page
\'normal\', // $context
\'high\'); // $priority
}
add_action(\'add_meta_boxes\', \'add_embed_recipe_meta_box\');
// Show the meta box in admin
function show_embed_recipe_meta_box() {
global $post;
$meta = get_post_meta($post->ID, \'recipe_embed\', true);
// use nonce for verification
echo \'<input type="hidden" name="custom_meta_box_nonce" value="\'.wp_create_nonce(basename(__FILE__)).\'" />\';
echo \'<table class="form-table">\';
echo \'<tr>
<th><label for="recipe_embed">Ingredients</label></th>
<td><textarea name="recipe_embed" id="recipe_embed" cols="60" rows="1">\'.$meta.\'</textarea></td>
</tr>\';
echo \'</table>\';
}
// Save the meta data
function save_recipe_embed($post_id) {
// verify nonce
if (!wp_verify_nonce($_POST[\'custom_meta_box_nonce\'], basename(__FILE__)))
return $post_id;
// check autosave
if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE)
return $post_id;
// check permissions
if (\'page\' == $_POST[\'post_type\']) {
if (!current_user_can(\'edit_page\', $post_id))
return $post_id;
} elseif (!current_user_can(\'edit_post\', $post_id)) {
return $post_id;
}
$old = get_post_meta($post_id, "recipe_embed", true);
$new = $_POST["recipe_embed"];
if ($new && $new != $old) {
update_post_meta($post_id, "recipe_embed", $new);
} elseif (\'\' == $new && $old) {
delete_post_meta($post_id, "recipe_embed", $old);
}
}
add_action(\'save_post\', \'save_recipe_embed\');
并使用以下代码调用元数据:
<?php $recipe_embed = get_post_meta($post->ID, "recipe_embed", true);
if($recipe_embed != ""){ //content for true if statement below ?>
<div style ="background: green;">
<?php echo $recipe_embed; //this is the content of the value ?>
</div>
<?php } //close the if statement ?>
在我的Metabox中添加一个字段并稍后显示该数据效果很好,但我在尝试向输入中添加数组时迷失了方向。非常感谢您的建议。或者其他更好的方法来完成相同的任务:)
谢谢
最合适的回答,由SO网友:jgraup 整理而成
您可以使用您所拥有的,只需稍加修改。只需增加元框的高度,让用户在每行输入一个项目即可。
嵌入
$recipe_embed = get_post_meta( $post->ID, "recipe_embed", true );
if ( ! empty( $recipe_embed ) ): ?>
<div style="background: green;">
<?php
// normalize returns
$recipe_embed = str_replace( "\\n", "\\r", $recipe_embed );
// convert to array and remove empty items
$ingredients = array_filter( explode( "\\r", $recipe_embed ) );
// list items
echo "<ul class=\'ingredients\'>";
foreach ( $ingredients as $ingredient ) {
echo "<li class=\'ingredient\' >$ingredient</li>";
}
echo "</ul>";
?>
</div>
<?php endif; // $recipe_embed
元框
<?php
function add_embed_recipe_meta_box() {
add_meta_box( \'embed_recipe_meta_box\', // $id
\'Recipe Post meta box\', // $title
\'show_embed_recipe_meta_box\', // $callback
\'post\', // $page
\'normal\', // $context
\'high\' );
}
add_action( "add_meta_boxes", "add_embed_recipe_meta_box" );
function show_embed_recipe_meta_box( $post ) {
$meta = get_post_meta( $post->ID, \'recipe_embed\', true );
wp_nonce_field( basename( __FILE__ ), "recipe-meta-box-nonce" );
?>
<table class="form-table">
<tr>
<th><label for="recipe_embed">Ingredients</label></th>
<td><textarea name="recipe_embed" id="recipe_embed" cols="60" rows="10"><?php echo $meta; ?></textarea></td>
</tr>
</table>
<?php
}
function save_recipe_embed( $post_id, $post, $update ) {
if ( ! isset( $_POST[ "recipe-meta-box-nonce" ] ) || ! wp_verify_nonce( $_POST[ "recipe-meta-box-nonce" ], basename( __FILE__ ) ) ) {
return $post_id;
}
if ( ! current_user_can( "edit_post", $post_id ) ) {
return $post_id;
}
if ( defined( "DOING_AUTOSAVE" ) && DOING_AUTOSAVE ) {
return $post_id;
}
$slug = "post";
if ( $slug != $post->post_type ) {
return $post_id;
}
$recipe_embed = \'\';
if ( isset( $_POST[ "recipe_embed" ] ) ) {
$recipe_embed = $_POST[ "recipe_embed" ];
}
update_post_meta( $post_id, "recipe_embed", $recipe_embed );
}
add_action( "save_post", "save_recipe_embed", 10, 3 );