2016-08-18 12 views
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'); 

但對於圖像?

enter image description here

任何想法?

回答

2

試試這個:在編輯圖像/附件頁面中添加metabox。

在當前活動主題的functions.php文件中添加以下代碼。

add_action('add_meta_boxes', 'add_attachment_metaboxes'); 

function add_attachment_metaboxes() { 
    add_meta_box('attachment_metaboxes', 'More Description', 'custom_attachment_metaboxes_func', 'attachment', 'normal', 'default'); 
} 
function custom_attachment_metaboxes_func() 
{ 
    global $post;  
    echo '<input type="hidden" name="meta_data_noncename" id="meta_data_noncename" value="'.wp_create_nonce('my_custom_nonce').'" />';  
    $metadata = get_post_meta($post -> ID, 'meta_data', true);  
    ?> 
    <div> 
     <table> 
      <tr valign="top"> 
       <td> 
        <input type="text" name="meta_data" id="meta_data" size="70" value="<?php echo $metadata; ?>" />     
       </td> 
      </tr> 
     </table>   
    </div> 
    <?php 
} 
function save_attachment_meta($post_id) { 
    if (!wp_verify_nonce($_POST['meta_data_noncename'],'my_custom_nonce')) 
    { 
     return $post -> ID; 
    }  
    if (!current_user_can('edit_post', $post -> ID)) 
    { 
      return $post -> ID;  
    } 
    if(isset($_POST["meta_data"])) 
    {  
     update_post_meta($post_id, "meta_data", $_POST["meta_data"]); 
    } 
} 
add_action('edit_attachment', 'save_attachment_meta'); 

Meta鍵是: meta_data

+0

感謝,但它不工作。根本看不到額外的領域。 – laukok

+1

我已經在demo wordpress中測試過。它正在工作@ teelou – vrajesh

+0

你是對的。我忘了複製這行'add_action('add_meta_boxes','add_attachment_metaboxes');'我的壞。謝謝。 – laukok

相關問題