1
我知道如何metabox添加文章和網頁:WordPress的 - 如何添加更多的元框圖像?
/**
* Add custom meta box to a specific page in the WP admin.
*
* @ http://themefoundation.com/wordpress-meta-boxes-guide/
* @ http://www.farinspace.com/page-specific-wordpress-meta-box/
*/
function homepage_featured_init() {
// Get post/page ID.
$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
// Get post/page slug.
$post = get_post($post_id);
$slug = $post->post_name;
// checks for post/page slug.
if ($slug == 'home') {
add_meta_box('homepage_meta_featured', __('Featured Events and Multimedia', 'homepage-featured'), 'homepage_featured_callback', array('post', 'page'));
}
add_action('add_metaboxe_featured', 'homepage_meta_featured');
}
add_action('admin_init','homepage_featured_init');
/**
* Outputs the content of the meta box.
*/
function homepage_featured_callback($post) {
// Better has an underscore as last sign.
$prefix = 'metriclife_';
wp_nonce_field(basename(__FILE__), 'homepage_featured_nonce');
$stored_meta = get_post_meta($post->ID);
?>
<div>
<div>
<textarea id="<?php echo $prefix; ?>meta_featured" name="<?php echo $prefix; ?>meta_featured" style="width:100%;" rows="10"/><?php if (isset ($stored_meta["{$prefix}meta_featured"])) echo $stored_meta["{$prefix}meta_featured"][0]; ?></textarea>
<?php //wp_editor($field_value_featured[0], "{$prefix}meta_featured", $args);?>
</div>
</div>
<?php
}
/**
* Saves the custom meta input.
*/
function homepage_meta_featured ($post_id) {
// Better has an underscore as last sign.
$prefix = 'metriclife_';
// Checks save status
$is_autosave = wp_is_post_autosave($post_id);
$is_revision = wp_is_post_revision($post_id);
$is_valid_nonce = (isset($_POST[ 'homepage_featured_nonce' ]) && wp_verify_nonce($_POST[ 'homepage_featured_nonce' ], basename(__FILE_))) ? 'true' : 'false';
// Exits script depending on save status
if ($is_autosave || $is_revision || !$is_valid_nonce) {
return;
}
// Checks for input and sanitizes/saves if needed
if(isset($_POST[ "{$prefix}meta_featured" ])) {
// Cleans your input.
// update_post_meta($post_id, "{$prefix}meta_featured", sanitize_text_field($_POST[ "{$prefix}meta_featured" ]));
// Stop wp_editor removes html tags.
update_post_meta($post_id, "{$prefix}meta_featured", stripslashes($_POST[ "{$prefix}meta_featured" ]));
}
}
add_action('save_post', 'homepage_meta_featured');
但對於圖像?
任何想法?
感謝,但它不工作。根本看不到額外的領域。 – laukok
我已經在demo wordpress中測試過。它正在工作@ teelou – vrajesh
你是對的。我忘了複製這行'add_action('add_meta_boxes','add_attachment_metaboxes');'我的壞。謝謝。 – laukok