Wordpress - 如何为图像添加更多元框?

时间:2016-08-18 10:04:43

标签: wordpress meta-boxes wordpress-4.5.2

我知道如何将元数据添加到帖子和页面:

/**
 * 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

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

试一下:在编辑图片/附件页面中添加元数据。

在当前有效主题的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_data