wordpress使用add_theme_support实现自定义头部图像

鱼见海资源网最新教程:

add_theme_support是wordpress的一个常用函数,让主题支持一些特定功能。

借助官网文档,具体看看如何实现WordPress主题头部图像自定义的。

从wp Version 3.4, 主题开始使用 add_theme_support() 在 functions.php 文件中,可以自定义头部的一些背景颜色,图像等等, 例如:

add_theme_support( 'custom-header' );

这样便会在WP后台-外观-出现-顶部菜单,从而进行头部图像自定义设置。

参数使用

$defaults = array(
	'default-image'          => '',
	'width'                  => 0,
	'height'                 => 0,
	'flex-height'            => false,
	'flex-width'             => false,
	'uploads'                => true,
	'random-default'         => false,
	'header-text'            => true,
	'default-text-color'     => '',
	'wp-head-callback'       => '',
	'admin-head-callback'    => '',
	'admin-preview-callback' => '',
);
add_theme_support( 'custom-header', $defaults );

实例

1.设置一个自定义头形象

设定默认头部图片 980px width, 60px height:

$args = array(
	'width'         => 980,
	'height'        => 60,
	'default-image' => get_template_directory_uri() . '/images/header.jpg',
);
add_theme_support( 'custom-header', $args );
Upload other custom header images

2.设置一个默认标题形象和允许网站所有者上传其他图片:

$args = array(
	'width'         => 980,
	'height'        => 60,
	'default-image' => get_template_directory_uri() . '/images/header.jpg',
	'uploads'       => true,
);
add_theme_support( 'custom-header', $args );

3.灵活头部设置

$args = array(
	'flex-width'    => true,
	'width'         => 980,
	'flex-height'    => true,
	'height'        => 200,
	'default-image' => get_template_directory_uri() . '/images/header.jpg',
);
add_theme_support( 'custom-header', $args );

头部文件 header.php调用

官方文档
https://codex.wordpress.org/Custom_Headers

文章来源于互联网

© 版权声明
THE END
喜欢就支持一下吧
点赞222 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容