簡體   English   中英

WordPress-如何向圖像添加更多元框?

[英]Wordpress - how to add more meta box to images?

我知道如何將metabox添加到帖子和頁面:

/**
 * 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');

但是圖像呢?

在此處輸入圖片說明

有任何想法嗎?

試試看 :在編輯圖像/附件頁面內添加metabox。

在當前actived主題的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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM