附件媒体-模板数据模型(data.size.url)

时间:2013-09-24 作者:Anneke

我正在尝试构建一个媒体库,用户可以在其中选择从自定义文件夹中检索的图片,用户可以指定其basefolder。

我希望尽可能多地使用WP功能,甚至使用WP的附件模板,因为只需要显示其他图像,但基本功能保持不变。

我遇到的问题是,WP不会显示数据。大小它的模板中的url。它保持为空。

这是WP模板(我省略了底部部分,因为它与本文无关):

<script type="text/html" id="tmpl-attachment">
        <div class="attachment-preview type-{{ data.type }} subtype-{{ data.subtype }} {{ data.orientation }}">
            <# if ( data.uploading ) { #>
                <div class="media-progress-bar"><div></div></div>
            <# } else if ( \'image\' === data.type ) { #>
                <div class="thumbnail">
                    <div class="centered">
                        <img src="{{ data.size.url }}" draggable="false" />
                    </div>
                </div>
            <# } else { #>
                <img src="{{ data.icon }}" class="icon" draggable="false" />
                <div class="filename">
                    <div>{{ data.filename }}</div>
                </div>
            <# } #>    
            <# if ( data.buttons.close ) { #>
                <a class="close media-modal-icon" href="#" title="<?php _e(\'Remove\'); ?>"></a>
            <# } #>    
            <# if ( data.buttons.check ) { #>
                <a class="check" href="#" title="<?php _e(\'Deselect\'); ?>"><div class="media-modal-icon"></div></a>
            <# } #>
        </div>
    </script>
这是我的目标:

$aFileImgSize = getimagesize($sFolderUrl);

$aImage = array();
$aImage[\'uploading\'] = false;
$aImage[\'id\'] = $sFolderUrl;
$aImage[\'full\'] = $sFolderUrl;
$aImage[\'thumbnail\'] = $sFolderUrl;
$aImage[\'link\'] = $sFolderUrl;
$aImage[\'caption\'] = $sFile;
$aImage[\'type\'] = \'image\';
$aImage[\'size\'][\'url\'] = \'test\';
$aImage[\'width\'] = $aFileImgSize[0];
$aImage[\'height\'] = $aFileImgSize[1];
$aImage[\'subtype\'] = $aFileImgSize[2];

if($aImage[\'width\'] >= $aImage[\'height\']){
    $sOrientation = \'landscape\';
}else{
    $sOrientation = \'portrait\';
}
$aImage[\'orientation\'] = $sOrientation;

$aSize = array();
$aSize[\'url\'] = $sFolderUrl;
$aSize[\'width\'] = $aFileImgSize[0];
$aSize[\'height\'] = $aFileImgSize[1];
$aSize[\'orientation\'] = $sOrientation;
$aImage[\'size\'] = $aSize;

$images[] = $aImage;
我好像错过了什么。。。

$images数组当然是用json\\u编码返回的

当我从数据更改WP模板时。大小指向数据的url。缩略图,显示正确的值。可能是一些愚蠢的事情,但我希望有人能指出我的愚蠢;)

有关此插件的前一篇文章可在此处找到:print_media_templates not applied in media manager plugin

1 个回复
SO网友:Anneke

问题已修复:我没有发送media\\u模板所需的所有属性(并且没有找到可能的属性列表)。

最后,我通过这样做(最后6行)来警告数据对象,首先签出“data”,然后签出返回的每个[对象,对象](例如data.size)

<script type="text/html" id="tmpl-attachment">
        <div class="attachment-preview type-{{ data.type }} subtype-{{ data.subtype }} {{ data.orientation }}">
            <# if ( data.uploading ) { #>
                <div class="media-progress-bar"><div></div></div>
            <# } else if ( \'image\' === data.type ) { #>
                <div class="thumbnail">
                    <div class="centered">
                        <img src="{{ data.size.url }}" draggable="false" />
                    </div>
                </div>
            <# } else { #>
                <img src="{{ data.icon }}" class="icon" draggable="false" />
                <div class="filename">
                    <div>{{ data.filename }}</div>
                </div>
            <# } #>    
            <# if ( data.buttons.close ) { #>
                <a class="close media-modal-icon" href="#" title="<?php _e(\'Remove\'); ?>"></a>
            <# } #>    
            <# if ( data.buttons.check ) { #>
                <a class="check" href="#" title="<?php _e(\'Deselect\'); ?>"><div class="media-modal-icon"></div></a>
            <# } #>
        </div>
        <#
                var o = data.size
                var out = \'\';
                for (var p in o) {
                  out += p + \': \' + o[p] + \'\\n\';
                }
                alert(out);
    </script>
对于任何想要获取完整属性列表的人,我将复制填充对象的代码

$aFileImgSize = getimagesize($sFolderUrl);

if($aFileImgSize[0] >= $aFileImgSize[1]){
    $sOrientation = \'landscape\';
}else{
    $sOrientation = \'portrait\';
}

$aImage = array();
$aImage[\'id\'] = $sFolderUrl;
$aImage[\'title\'] = $sFile;
$aImage[\'filename\'] = $sFile;
$aImage[\'url\'] = $sFolderUrl;
$aImage[\'link\'] = $sFolderUrl;
$aImage[\'alt\'] = $sFile;
$aImage[\'author\'] = \'3\';
$aImage[\'description\'] = $sFile;
$aImage[\'caption\'] = $sFile;
$aImage[\'name\'] = $sFile;
$aImage[\'status\'] = \'inherit\';
$aImage[\'uploadedTo\'] = \'0\';
$aImage[\'date\'] = \'\';
$aImage[\'modified\'] = \'\';
$aImage[\'menuOrder\'] = \'0\';
$aImage[\'mime\'] = \'image/\'.$aFileImgSize[2];
$aImage[\'type\'] = \'image\';
$aImage[\'subtype\'] = $aFileImgSize[2];
$aImage[\'icon\'] = \'\';
$aImage[\'dateFormatted\'] = \'\';
    $aNonces = array();
    $aNonces[\'update\'] = \'\';
    $aNonces[\'delete\'] = \'\';
$aImage[\'nonces\'] = $aNonces;//object
$aImage[\'editLink\'] = \'\';
    $aSizes = array();
        $aSizesThumbnail = array();
        $aSizesThumbnail[\'height\'] = $aFileImgSize[1];
        $aSizesThumbnail[\'width\'] = $aFileImgSize[0];
        $aSizesThumbnail[\'url\'] = $sFolderUrl;
        $aSizesThumbnail[\'orientation\'] = $sOrientation;
    $aSizes[\'thumbnail\'] = $aSizesThumbnail;//object
        $aSizesMedium = array();
        $aSizesMedium[\'height\'] = $aFileImgSize[1];
        $aSizesMedium[\'width\'] = $aFileImgSize[0];
        $aSizesMedium[\'url\'] = $sFolderUrl;
        $aSizesMedium[\'orientation\'] = $sOrientation;
    $aSizes[\'medium\'] = $aSizesMedium;//object
        $aSizesFull = array();
        $aSizesFull[\'height\'] = $aFileImgSize[1];
        $aSizesFull[\'width\'] = $aFileImgSize[0];
        $aSizesFull[\'url\'] = $sFolderUrl;
        $aSizesFull[\'orientation\'] = $sOrientation;
    $aSizes[\'full\'] = $aSizesFull;//object
$aImage[\'sizes\'] = $aSizes;//object
$aImage[\'height\'] = $aFileImgSize[1];
$aImage[\'width\'] = $aFileImgSize[0];
$aImage[\'orientation\'] = $sOrientation;
    $aCompat = array();
    $aCompat[\'item\'] = \'\';
    $aCompat[\'meta\'] = \'\';
$aImage[\'compat\'] = $aCompat;//object
$aImage[\'uploading\'] = \'\';
    $aButtons = array();
    $aButtons[\'check\'] = true;
$aImage[\'buttons\'] = $aButtons;//object
$aImage[\'describe\'] = \'\';
    $aSize = array();
    $aSize[\'url\'] = $sFolderUrl;
    $aSize[\'width\'] = $aFileImgSize[0];
    $aSize[\'height\'] = $aFileImgSize[1];
    $aSize[\'orientation\'] = $sOrientation;
$aImage[\'size\'] = $aSize;//object
    $aCan = array();
    $aCan[\'remove\'] = true;
    $aCan[\'save\'] = true;
$aImage[\'can\'] = $aCan;//object
$aImage[\'allowLocalEdits\'] = true;
顺便说一下:如果任何人有解释这一点的来源,一定要对此发表评论。我找不到它,而且我对脊梁骨也不太熟悉。js和wordpress核心,所以我只是发布我解决这个问题的方法。

接下来:找出为什么我自己的图片没有被插入帖子/页面

结束