当前位置: 首页>>建站开发>>正文


如何在媒体库中显示.ico文件

webfans 建站开发 , , 去评论

问题描述

我在我的WordPress网站上添加了一个.ico mime类型,我可以上传favicon文件。但是媒体库仅显示这些图像的default.png图像以及定制器中的图像。如何让WordPress在媒体库和自定义程序中显示这些图标图像?

最佳解决方案

更新:看起来这将在5.0+中得到支持。见票据#43458

默认值

这就是favicon(.ico)文件在Media Grid视图中的显示方式:

media-library,favicon,wordpress

这是微模板的相应部分:

<# } else if ( 'image' === data.type && data.sizes ) { #>
    <div class="centered">
        <img src="{{ data.size.url }}" draggable="false" alt="" />
    </div>
<# } else { #>
    <div class="centered"> 
        <# if ( data.image && data.image.src && data.image.src !== data.icon ) { #>
           <img src="{{ data.image.src }}" class="thumbnail" draggable="false" />
        <# } else { #>
            <img src="{{ data.icon }}" class="icon" draggable="false" />
        <# } #>
    </div>
    <div class="filename">
        <div>{{ data.filename }}</div>
    </div>
<# } #>

其中data.sizes为favicons为空。

方法1)使用wp_mime_type_icon过滤器

favicons的mime类型是image/x-icon

我设法在Media Grid视图中显示.ico文件:

add_filter( 'wp_mime_type_icon', function( $icon, $mime, $post_id )
{
    if( $src = false || 'image/x-icon' === $mime && $post_id > 0 )
        $src = wp_get_attachment_image_src( $post_id );

    return is_array( $src ) ? array_shift( $src ) : $icon;
}, 10, 3 ); 

这里将wp_get_attachment_image_src的第三个参数保持为$icon = false(默认情况下)以避免无限循环非常重要!

然后显示这样的favicons:

media-library,favicon,wordpress

方法2)使用wp_prepare_attachment_for_js过滤器

当我们加载媒体网格视图时,我们调用wp_ajax_query_attachments处理程序。它执行以下附件查询:

$query = new WP_Query( $query );
$posts = array_map( 'wp_prepare_attachment_for_js', $query->posts );

在此wp_prepare_attachment_for_js功能中,各种信息被添加到WP_Post帖子中,并使用以下内容进行过滤:

return apply_filters( 'wp_prepare_attachment_for_js', $response, $attachment, $meta );

其中输出是$response数组。

我们可以使用此过滤器为favicon添加缺少的大小:

add_filter( 'wp_prepare_attachment_for_js', function( $response, $attachment, $meta )
{
    if( 'image/x-icon' === $response['mime'] 
         && isset( $response['url'] )
         && ! isset( $response['sizes']['full'] )
    )
    {
            $response['sizes'] = array( 'full' => array( 'url' => $response['url'] ) );
    }   
   return $response;
}, 10, 3 );

然后他们就会出现这样的情况:

media-library,favicon,wordpress

请注意,我们只设置了url部分而不是widthheightorientation。例如,我们可以借助wp_get_attachment_image_src()功能进一步扩展解决方案以添加此数据。但我把它留给你;-)

一些$response示例:

以下是favicon.ico文件的$response数组示例:

Array 
(
    [id] => 803
    [title] => favicon
    [filename] => favicon.ico
    [url] => http://example.tld/wp-content/uploads/2015/02/favicon.ico
    [link] => http://example.tld/?attachment_id=803
    [alt] => 
    [author] => 11
    [description] => 
     => 
    [name] => favicon
    [status] => inherit
    [uploadedTo] => 0
    [date] => 1423791136000
    [modified] => 1423791136000
    [menuOrder] => 0
    [mime] => image/x-icon
    [type] => image
    [subtype] => x-icon
    [icon] => http://example.tld/wp-includes/images/media/default.png
    [dateFormatted] => February 13, 2015
    [nonces] => Array
        (
            [update] => 4fac983f49
            [delete] => efd563466d
            [edit] => df266bf556
        )

    [editLink] => http://example.tld/wp-admin/post.php?post=803&action=edit
    [meta] => 
    [authorName] => someuser
    [filesizeInBytes] => 1406
    [filesizeHumanReadable] => 1 kB
    [compat] => Array
        (
            [item] => 
            [meta] => 
        )

)

以下是WordPress-Logo.jpg图像的示例:

Array
(
    [id] => 733
    [title] => WordPress-Logo
    [filename] => WordPress-Logo.jpg
    [url] => http://example.tld/wp-content/uploads/2015/02/WordPress-Logo.jpg
    [link] => http://example.tld/2015/02/10/test/wordpress-logo/
    [alt] => 
    [author] => 1
    [description] =>
     =>
    [name] =>  wordpress-logo
    [status] => inherit
    [uploadedTo] => 784
    [date] => 1423314735000
    [modified] => 1423571320000
    [menuOrder] => 0
    [mime] => image/jpeg
    [type] => image
    [subtype] => jpeg
    [icon] => http://example.tld/wp-includes/images/media/default.png
    [dateFormatted] => February 7, 2015
    [nonces] => Array
        (
            [update] => cb6a4bca10
            [delete] => 068a4d3897
            [edit] => 14b7d201ff
        )

    [editLink] => http://example.tld/wp-admin/post.php?post=733&action=edit
    [meta] => 
    [authorName] => someuser
    [uploadedToLink] => http://example.tld/wp-admin/post.php?post=784&action=edit
    [uploadedToTitle] => 20150209021847
    [filesizeInBytes] => 127668
    [filesizeHumanReadable] => 125 kB
    [sizes] => Array
        (
            [thumbnail] => Array
                (
                    [height] => 150
                    [width] => 150
                    [url] => http://example.tld/wp-content/uploads/2015/02/WordPress-Logo-150x150.jpg
                    [orientation] => landscape
                )

            [medium] => Array
                (
                    [height] => 184
                    [width] => 300
                    [url] => http://example.tld/wp-content/uploads/2015/02/WordPress-Logo-300x184.jpg
                    [orientation] => landscape
                )

            [full] => Array
                (
                    [url] => http://example.tld/wp-content/uploads/2015/02/WordPress-Logo.jpg
                    [height] => 620
                    [width] => 1010
                    [orientation] => landscape
                )

        )

    [height] => 620
    [width] => 1010
    [orientation] => landscape
    [compat] => Array
        (
            [item] => 
            [meta] => 
        )

)

ps:我们对这些例子中的$response['size']部分特别感兴趣。

参考资料

本文由朵颐IT整理自网络, 文章地址: https://duoyit.com/article/2285.html,转载请务必附带本地址声明。