Wordpress 将图片上传到同一帖子的 4 个不同的元框

问题描述

所以我目前正在处理一个自定义主题和 single.PHP 的编辑帖子,我有 4 个元框。我需要能够在每个元框上上传 1 张图片。是否有可能?我必须有哪些选项才能在同一个帖子上上传 4 张不同的图片?以及如何从 single.PHP 调用这些图像。

每个元框需要有 3 个字段,其中一个字段需要是每个组的图像。

这是我目前编辑帖子的代码...

<?PHP

/*
 * Function remove editor Meta Boxes
 */
function remove_Meta_Boxes()
{
    remove_post_type_support('post','editor');
    remove_post_type_support('post','trackbacks');
    remove_post_type_support('post','custom-fields');
    remove_post_type_support('post','comments');
    add_theme_support( 'post-thumbnails' );
}

/*
 *  Create one or more Meta Boxes to be displayed on the post editor screen.
 */
function add_mb_links()
{
    add_Meta_Box(
        'add-mb-links-class',// MetaBox ID
        'Grupo 1',// title
        'add_mb_links_callback',// callback function
        'post',// post type or post types in array
        'normal' // position (normal,side,advanced)
    //'default' // priority (default,low,high,core)
    );
}

/*
 * display the post Meta Box fields.
 */
function add_mb_links_callback($post)
{

    $grupo1 = get_post_meta($post->ID,'mb_grupo_1',true);
    $grupo2 = get_post_meta($post->ID,'mb_grupo_2',true);
    wp_nonce_field(basename(__FILE__),'mb_links_class_nonce');
    ?>

    <p>
        <label for="grupo1-mb-links-class">Nombre del Grupo 1</label>
        <br/>
        <input class="widefat" type="text" name="grupo1-mb-links-class" id="grupo1-mb-links-class"
               value="<?PHP echo esc_attr($grupo1); ?>" size="30"/>
    </p>

    <p>
        <label for="grupo2-mb-links-class">Nombre del Grupo 2.</label>
        <br/>
        <textarea rows="10" class="widefat" type="text" name="grupo2-mb-links-class" id="grupo2-mb-links-class" size="30"><?PHP echo esc_attr($grupo2); ?></textarea>
    </p>

<?PHP }

/*
 * Save,update,delete the Meta
 * Box's post Metadata
 */
function mb_links_save_class($post_id,$post)
{

    // Verify the nonce before proceeding.
    if (!isset($_POST['mb_links_class_nonce']) || !wp_verify_nonce($_POST['mb_links_class_nonce'],basename(__FILE__))) {
        return $post_id;
    }

    // Get the post type object.
    $post_type = get_post_type_object($post->post_type);

    // Check if the current user has permission to edit the post.
    if (!current_user_can($post_type->cap->edit_post,$post_id)) {
        return $post_id;
    }

    // Get the posted data and sanitize it for use as an HTML class.
    $new_youtube_Meta_value = (isset($_POST['grupo1-mb-links-class']) ? $_POST['grupo1-mb-links-class'] : ’);
    $new_spreaker_Meta_value = (isset($_POST['grupo2-mb-links-class']) ? $_POST['grupo2-mb-links-class'] : ’);

    // Get the Meta key.
    $youtube_Meta_key = 'mb_grupo_1';
    $spreaker_Meta_key = 'mb_grupo_2';

    // Get the Meta value of the custom field key.
    $youtube_Meta_value = get_post_meta($post_id,$youtube_Meta_key,true);
    $spreaker_Meta_value = get_post_meta($post_id,$spreaker_Meta_key,true);

    //Youtube Meta data - add,delete
    // If a new Meta value was added and there was no prevIoUs value,add it.
    if ($new_youtube_Meta_value && ’ == $youtube_Meta_value) {
        add_post_Meta($post_id,$new_youtube_Meta_value,true);
    } // If the new Meta value does not match the old value,update it.
    elseif ($new_youtube_Meta_value && $new_youtube_Meta_value != $youtube_Meta_value) {
        update_post_Meta($post_id,$new_youtube_Meta_value);
    } // If there is no new Meta value but an old value exists,delete it.
    elseif ($new_youtube_Meta_value == '') {
        delete_post_Meta($post_id,$youtube_Meta_value);
    }

    //Spreaker Meta data - add,add it.
    if ($new_spreaker_Meta_value && ’ == $spreaker_Meta_value) {
        add_post_Meta($post_id,$new_spreaker_Meta_value,update it.
    elseif ($new_spreaker_Meta_value && $new_spreaker_Meta_value != $spreaker_Meta_value) {
        update_post_Meta($post_id,$new_spreaker_Meta_value);
    } // If there is no new Meta value but an old value exists,delete it.
    elseif ($new_spreaker_Meta_value == '') {
        delete_post_Meta($post_id,$spreaker_Meta_value);
    }
}

add_action('load-post.PHP','add_mb_links');
add_action('load-post-new.PHP','add_mb_links');
add_action('add_Meta_Boxes','add_mb_links');
add_action('save_post','mb_links_save_class',10,2);
add_action('init','remove_Meta_Boxes');

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)