php - add_image_size 用于 Wordpress 中的自定义 Logo

标签 php wordpress

我正在使用以下代码使用户能够为我的 wordpress 主题上传自定义 Logo :

function mytheme_setup() {
    add_theme_support('custom-logo');
}

add_action('after_setup_theme', 'mytheme_setup');

我已经尝试使用 add_image_size 来渲染 180x180px 的标志:

add_image_size('mytheme-logo', 180, 180);
add_theme_support('custom-logo', array(
    'size' => 'mytheme-logo'
));

为了在我的主题中显示 Logo ,我使用了:

<?php
    // Display the Custom Logo
    the_custom_logo('mytheme-logo');
    // No Custom Logo, just display the site's name
    if (!has_custom_logo()) {
?>
        <h1 style="text-align:center;"><?php bloginfo('name'); ?></h1>
        <?php
    }
?>

所以我现在可以上传并显示 Logo 了,但图像似乎不受 add_image_size 的影响。 wordrpress 未调整图像大小,这是一个问题,因为它可能会影响网站的加载时间。

我是不是做错了什么?

最佳答案

不幸的是,自定义 Logo add_theme_support() 调用不再支持直接引用自定义图像尺寸:

Custom Logo does not support the size argument anymore. Specifying the logo size in individual arguments has the advantage of only creating a special image file for the specific logo used (and not every image uploaded), and providing more parity with how Custom Header works.

(来自 Custom Logo on Make Wordpress Core)

但是,这是一个简单的 - 并且可以说是更好的 - 上述修复:只需添加宽度和高度作为参数:

add_theme_support('custom-logo', array(
    'width' => 180,
    'height' => 180,
));

然后您可以完全放弃对 add_image_size() 的调用。

关于php - add_image_size 用于 Wordpress 中的自定义 Logo ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37535317/

相关文章:

Javascript parseInt parseFloat 和舍入小数

php - 构建自定义 Laravel 数据库适配器

php - 如何在Woocommerce中检查产品是否具有特定的产品属性

javascript - 不安全的 <form> 调用。在 index.html 页面上找到

php - 通过自定义前端表单设置 Woocommerce 产品标签和类别

php - 如何使用 AWS SDK PHP 将触发器添加到 AWS Lambda 函数?

php - Ajax 安全(我希望)

php - 如何让 EXIF PHP 扩展在 Heroku 上工作?

php - 脚本和样式未在 wordpress 中加载

jquery - 循环内的 WordPress 循环无法正常工作