我正在使用以下代码使用户能够为我的 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/