首页 问答 正文

如何为WordPress主题添加自定义背景(background)?

注册会员 / 悠然自学 / 2023-07-11/ 浏览 132 次

要为WordPress主题添加自定义背景,你可以按照以下步骤进行操作:

  1. 在主题的functions.php文件中添加以下代码:
function custom_theme_support() {
    $args = array(
        'default-color' => '',
        'default-image' => '',
        'default-repeat' => '',
        'default-position-x' => '',
        'default-position-y' => '',
        'default-size' => '',
        'default-attachment' => '',
    );
    add_theme_support( 'custom-background', $args );
}
add_action( 'after_setup_theme', 'custom_theme_support' ); 
  1. 在主题的style.css文件中添加以下代码:
body {
    background-color: #ffffff;
    background-image: url('path/to/background-image.jpg');
    background-repeat: repeat;
    background-position: left top;
    background-size: auto;
    background-attachment: scroll;
} 

请注意,将path/to/background-image.jpg替换为你想要使用的背景图片的实际路径。

  1. 保存修改后的文件并将主题上传到你的WordPress网站。

此时,你的WordPress主题就会添加了自定义背景。请根据你的需要修改背景颜色、图片路径、重复方式、位置、大小和附着方式等属性。

大家谈论
    我的见解