简体   繁体   中英

add_image_size for custom logo in Wordpress

I am using the following code to enable users to upload a custom logo for my wordpress theme:

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

add_action('after_setup_theme', 'mytheme_setup');

I have tried implementing add_image_size to render the logo at 180x180px with this:

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

To display the logo in my theme, I have used:

<?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
    }
?>

So I am able to upload and display the logo now, but it seems that the image is not affected by the add_image_size . The image is not resized by wordrpress and this is a problem because it may affect loading time of the website.

Am I doing something wrong here?

Unfortunately, referring directly to a custom image size isn't supported anymore for Custom Logo add_theme_support() calls:

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.

(from Custom Logo on Make Wordpress Core )

However, it's an easy - and arguably superior - fix described above: just add the width and height as arguments instead:

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

You can then drop your call to add_image_size() completely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM