首页 问答 正文

如何在wordpress主题中添加自定义插图?

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

在WordPress主题中添加自定义插图,需要在主题的代码中添加相应的功能函数和调用方式。

以下是示例代码:

  1. 在主题的functions.php文件中添加以下代码:
function custom_image_header() {
    $args = array(
        'width'         => 1280,
        'height'        => 720,
        'default-image' => get_template_directory_uri() . '/images/header-image.jpg',
        'uploads'       => true,
    );
    add_theme_support( 'custom-header', $args );
}
add_action( 'after_setup_theme', 'custom_image_header' ); 
  1. 在主题的header.php文件中添加以下代码:
<?php if ( get_header_image() ) : ?>
    <img src="<?php echo esc_url( get_header_image() ); ?>" alt="">
<?php endif; ?> 

这里的示例代码使用了WordPress提供的自定义头部图片功能,设置默认尺寸为1280x720,默认图片路径为主题文件夹中的/images/header-image.jpg。在header.php文件中,使用了get_header_image()函数获取自定义头部图片,并通过img标签显示在网页中。

需要注意的是,如果要使用自定义插图,则必须在主题中包含相应的图片文件。使用get_template_directory_uri()函数获取主题路径,然后使用/images/header-image.jpg表示相对路径。

大家谈论
    我的见解