加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > CMS系统 > wordpress > 正文

wordpress自定义网站背景颜色和图片

发布时间:2020-12-14 14:35:17 所属栏目:wordpress 来源:网络整理
导读:如果你想改变默认的body的背景颜色,图片,参数,下面的代码轻松实现,这里只是一个参考,你完全可以自己增加css元素 在 functions.php 文件添加如下代码: // 激活自定义背景和设置回调函数 if ( function_exists( 'add_theme_support' ) ) { $defaults = a

如果你想改变默认的body的背景颜色,图片,参数,下面的代码轻松实现,这里只是一个参考,你完全可以自己增加css元素

在 functions.php 文件添加如下代码:

// 激活自定义背景和设置回调函数

if ( function_exists( 'add_theme_support' ) ) {

$defaults = array(

'default-color' => '000000',//自定义颜色

'default-image' => get_template_directory_uri() . '/img/background.png',//自定义图片

'wp-head-callback' => 'my_theme_background_cb',

'admin-head-callback' => '',

'admin-preview-callback' => ''

);

add_theme_support( 'custom-background',$defaults );

}

// Callback function to alter custom background behavior

function my_theme_background_cb() {

$background = get_background_image();

$color = get_background_color();

?

if ( ! $background && ! $color )

return;

?

$style = $color ? : '';

?

if ( $background ) {

$image = ;

?

$repeat = get_theme_mod( 'background_repeat','repeat' );

if ( ! in_array( $repeat,array( 'no-repeat','repeat-x','repeat-y','repeat' ) ) )

$repeat = 'repeat';

$repeat = ;

?

$position = get_theme_mod( 'background_position_x','left' );

if ( ! in_array( $position,array( 'center','right','left' ) ) )

$position = 'left';

$position = ;

?

$attachment = get_theme_mod( 'background_attachment','scroll' );

if ( ! in_array( $attachment,array( 'fixed','scroll' ) ) )

$attachment = 'scroll';

$attachment = ;

?

$style .= $image . $repeat . $position . $attachment;

}

?>