wordpress实现logo上传的方法有很多今天我们讲一下如何用内置的API实现logo上传。

在functions.php里添加如下代码

function themename_custom_logo_setup()
{
    $defaults = array(
        'height'      => 100,
        'width'       => 400,
        'flex-height' => true,
        'flex-width'  => true,
        'header-text' => array('site-title', 'site-description'),
    );
    add_theme_support('custom-logo', $defaults);
}
add_action('after_setup_theme', 'themename_custom_logo_setup');

在需要调用的位置(通常是header.php)里插入如下代码

$custom_logo_id = get_theme_mod(‘custom_logo’);$logo = wp_get_attachment_image_src($custom_logo_id, ‘full’);

$custom_logo_id = get_theme_mod('custom_logo');
$logo = wp_get_attachment_image_src($custom_logo_id, 'full');


if (has_custom_logo()) {
    printf('<img src="%s" alt="%s">', esc_url($logo[0]), get_bloginfo('name'));
} else {
    echo '<h1>' . '请上传logo' . '</h1>';
}

上方代码解释:

首先判断是否有上传logo,如果上传logo则显示logo,如果没上传logo则提示上传logo

在后台 【外观】->【自定义】->【站点身份】->【图标】->【选择图标】上传定义logo

发表回复

后才能评论