0
你好我試圖在wordpress中創建一個自定義字段面板。但是當我在函數中添加代碼時,任何人都可以幫我一把嗎?如何:在wordpress中自定義字段面板
這是我試圖
的代碼,但沒有在管理面板出現
$key = "videojuegos";
$meta_boxes = array(
"plataforma" => array(
"nombre" => "plataforma",
"titulo" => "Plataforma",
"descripcion" => "Plataforma del videojuego - XBox360, Playstation3, PC, etc..."),
"precio" => array(
"nombre" => "precio",
"titulo" => "Precio",
"descripcion" => "Precio del videojuego en Euros."),
"edad" => array(
"nombre" => "edad",
"titulo" => "Edad Recomendada",
"descripcion" => "Edad recomendada del videojuego"),
"idioma" => array(
"nombre" => "idioma",
"titulo" => "Idioma",
"descripcion" => "Idioma del videojuego")
);
function crear_meta_box() {
global $key;
if(function_exists('add_meta_box')) {
add_meta_box('nuevo-meta-boxes', ucfirst($key) . ' Características', 'mostrar_meta_box', 'videojuegos', 'normal', 'high');
}
}
function mostrar_meta_box() {
global $post, $meta_boxes, $key;
?>
<div class="form-wrap">
<?php
wp_nonce_field(plugin_basename(__FILE__), $key . '_wpnonce', false, true);
foreach($meta_boxes as $meta_box) {
$data = get_post_meta($post->ID, $key, true);
?>
<div class="form-field form-required">
<label for="<?php echo $meta_box[ 'nombre' ]; ?>"><?php echo $meta_box[ 'titulo' ]; ?></label>
<input type="text" name="<?php echo $meta_box[ 'nombre' ]; ?>" value="<?php echo htmlspecialchars($data[ $meta_box[ 'nombre' ] ]); ?>" />
<p><?php echo $meta_box[ 'descripcion' ]; ?></p>
</div>
<?php } // Fin del foreach?>
</div>
<?php
} // Fin de la función mostrar_meta_box
function dirigido_custom_box_mostrar($post) {
$valor_dirigido = get_post_meta($post->ID, 'valor_dirigido', true);
wp_nonce_field('save_dirigido_meta', 'dirigido_nonce');
?>
<?php wp_editor($valor_dirigido, 'valor_dirigido', array('media_buttons' => false, 'textarea_name' => 'valor_dirigido')); ?>
<?php
}
function grabar_meta_box($post_id) {
global $post, $meta_boxes, $key;
foreach($meta_boxes as $meta_box) {
$data[ $meta_box[ 'nombre' ] ] = $_POST[ $meta_box[ 'nombre' ] ];
}
if (!wp_verify_nonce($_POST[ $key . '_wpnonce' ], plugin_basename(__FILE__)))
return $post_id;
if (!current_user_can('edit_post', $post_id))
return $post_id;
update_post_meta($post_id, $key, $data);
}
add_action('admin_menu', 'crear_meta_box');
add_action('save_post', 'grabar_meta_box');
總是使用條件檢查與function_exists()* BEFORE *創建一個函數,以避免函數名稱衝突。 – bodi0
非常好... 感謝@bodi爲您的建議。 –