我创建了图库metabox,但它不起作用

时间:2016-06-04 06:23:04

标签: wordpress

这是我从github页面获得的代码:

define('MY_POST_TYPE', 'my');
  define('MY_POST_SLUG', 'gallery');
  function my_register_post_type () {
    $args = array (
        'label' => 'Gallery',
        'supports' => array( 'title', 'excerpt' ),
        'register_meta_box_cb' => 'my_meta_box_cb',
        'show_ui' => true,
        'query_var' => true
    );
    register_post_type( MY_POST_TYPE , $args );
  }
  add_action( 'init', 'my_register_post_type' );
  function my_meta_box_cb () {
    add_meta_box( MY_POST_TYPE . '_details' , 'Media Library', 'my_meta_box_details', MY_POST_TYPE, 'normal', 'high' );
  }
  function my_meta_box_details () {
    global $post;
    $post_ID = $post->ID; // global used by get_upload_iframe_src
    printf( "<iframe frameborder='0' src=' %s ' style='width: 100%%; height: 400px;'> </iframe>", get_upload_iframe_src('media') );
  }

它在该页面上很好地显示了我的图库页面和媒体上传部分,但是在该上传器中没有插入帖子按钮,我无法将图像插入该帖子。此外,如果我从我的计算机上传图像,它已插入到发布按钮,一旦我点击该按钮页面,我的所有媒体上载屏幕都会消失,没有任何反应?我的代码或方法有什么问题

1 个答案:

答案 0 :(得分:0)

您的支持中缺少editor

define('MY_POST_TYPE', 'my');
define('MY_POST_SLUG', 'gallery');
function my_register_post_type () {
    $args = array (
        'label' => 'Gallery',
        'supports' => array( 'title', 'excerpt', 'editor' ),
        'register_meta_box_cb' => 'my_meta_box_cb',
        'show_ui' => true,
        'query_var' => true
        );
    register_post_type( MY_POST_TYPE , $args );
}
add_action( 'init', 'my_register_post_type' );
function my_meta_box_cb () {
    add_meta_box( MY_POST_TYPE . '_details' , 'Media Library', 'my_meta_box_details', MY_POST_TYPE, 'normal', 'high' );
}

function my_meta_box_details () {
    global $post;
$post_ID = $post->ID; // global used by get_upload_iframe_src
printf( "<iframe frameborder='0' src=' %s ' style='width: 100%%; height: 400px;'> </iframe>", get_upload_iframe_src('media') );
}

添加编辑器后,单击“插入帖子”可以在编辑器中看到插入的链接。这大概就是你所需要的。

上传图片时,元数据框消失了。

在我看来,你并不需要元数据中的额外上传器。您在编辑器上方有“添加媒体”按钮 - 您可以通过这种方式在CPT中添加图像,视频或图库。

修改

您可以放弃媒体上传器,只需像这样调用默认的wp.media

function my_meta_box_details () {
    global $post;
    // Here we get the current images ids of the gallery
    $custom = get_post_custom($post->ID);
    $my_gallery = (isset($custom["my_gallery"][0])) ? $custom["my_gallery"][0] : '';

    // We display the gallery
    ?>
    <style>
        .gallery-item{
            width:150px;
            display: inline-block;
            margin-right: 10px;
        }
        .gallery-item img{
            width: 150px;
        }
    </style>
    <div class="gallery_images">
        <?php
        $img_array = (isset($my_gallery) && $my_gallery != '') ? explode(',', $my_gallery) : '';
        if ($img_array != '') {
            foreach ($img_array as $img) {
                echo '<div class="gallery-item">'.wp_get_attachment_image($img).'</div>';
            }
        }
        ?>
    </div>
    <p class="separator">
        <input id="my_gallery_input" type="hidden" name="my_gallery" value="<?php echo $my_gallery; ?>" data-urls=""/>
        <input id="manage_gallery" title="Manage gallery" type="button" value="Manage gallery" />
        <input id="empty_gallery" title="Empty gallery" type="button" value="Empty gallery" />
    </p>
    <script>
    jQuery(document).ready(function($) {
        $(document).on('click', '#manage_gallery', upload_gallery_button);

        function upload_gallery_button(e) {
            e.preventDefault();
            var $input_field = $('#my_gallery_input');
            var ids = $input_field.val();
            var gallerysc = '[gallery ids="' + ids + '"]';
            wp.media.gallery.edit(gallerysc).on('update', function(g) {
                var id_array = [];
                var url_array = [];
                $.each(g.models, function(id, img){
                    url_array.push(img.attributes.url);
                    id_array.push(img.id);
                });
                var ids = id_array.join(",");
                ids = ids.replace(/,\s*$/, "");
                var urls = url_array.join(",");
                urls = urls.replace(/,\s*$/, "");
                $input_field.val(ids);
                var html = '';
                for(var i = 0 ; i < url_array.length; i++){
                    html += '<div class="gallery-item"><img src="'+url_array[i]+'"></div>';
                }

                $('.gallery_images').html('').append(html);
            });

        }

        $(document).on('click', '#empty_gallery', empty_gallery_button);

        function empty_gallery_button(e){
            e.preventDefault();
            var $input_field = $('#my_gallery_input');
            $input_field.val('');
            $('.gallery_images').html('');
        }
    });
    </script>
    <?php

}

function my_metabox_save(){
    global $post;
    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
        return;
    } elseif(is_object($post)){
        $my_gallery = (isset($_POST['my_gallery'])) ? $_POST['my_gallery'] : '';
        update_post_meta($post->ID, 'my_gallery', $my_gallery);
    }
}

add_action('save_post', 'my_metabox_save');

editor参数中删除supports。应该工作正常。