WordPress主题制作

  1. 主页
  2. 文档
  3. WordPress主题制作
  4. 自定义Logo
  5. 为主题添加自定义徽标支持

为主题添加自定义徽标支持

使用add_theme_support()将自定义徽标支持首先添加到您的主题中, :

add_theme_support( 'custom-logo' );

启用自定义Logo支持时,可以使用数组将参数传递给add_theme_support()函数来配置五个参数:

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' );

参数:

height
预期徽标高度(以像素为单位)。自定义徽标也可以使用内置的图像尺寸,例如thumbnail,或使用add_image_size() 来注册自定义尺寸。
width
预期徽标宽度(以像素为单位)。自定义徽标也可以使用内置的图像尺寸,例如thumbnail,或使用add_image_size() 来注册自定义尺寸 。
flex-height
是否允许灵活的高度。
flex-width
是否允许灵活的宽度。
header-text
要隐藏的元素的类。它可以在此处为构成页眉文本的所有元素传递一个类名数组,这些页眉文本可以用徽标替换。

然后the_custom_logo在主题中调用

自定义Logo是可选的,但如果主题作者在其主题中包含Logo,则应使用此功能。

// Custom logo.
	$logo_width  = 120;
	$logo_height = 90;

	// If the retina setting is active, double the recommended width and height.
	if ( get_theme_mod( 'retina_logo', false ) ) {
		$logo_width  = floor( $logo_width * 2 );
		$logo_height = floor( $logo_height * 2 );
	}

	add_theme_support(
		'custom-logo',
		array(
			'height'      => $logo_height,
			'width'       => $logo_width,
			'flex-height' => true,
			'flex-width'  => true,
		)
	);

使用after_setup_theme钩子,以便在主题加载后注册自定义Logo支持。

这篇文章对您有用吗?

我们要如何帮助您?