wordpress功能集成(十五):主题自定义头部功能

新版本的wordpress自定义头部功能和自定义背景很像,自3.4版本后,为主题添加自定义头部功能方法很简单那,只需要往主题的functions.php中添加代码:

  1. add_theme_support( 'custom-header' );  

同样的,你也可以使用一些参数来设置默认选项:

  1. $defaults = array(   
  2.     'default-image'          => '',   
  3.     'random-default'         => false,   
  4.     'width'                  => 0,   
  5.     'height'                 => 0,   
  6.     'flex-height'            => false,   
  7.     'flex-width'             => false,   
  8.     'default-text-color'     => '',   
  9.     'header-text'            => true,   
  10.     'uploads'                => true,   
  11.     'wp-head-callback'       => '',   
  12.     'admin-head-callback'    => '',   
  13.     'admin-preview-callback' => '',   
  14. );   
  15. add_theme_support( 'custom-header', $defaults );  

实例:设置一个默认的头部图像,

  1. $args = array(   
  2.     'width'         => 980,   
  3.     'height'        => 60,   
  4.     'default-image' => get_template_directory_uri() . '/images/header.jpg',   
  5. );   
  6. add_theme_support( 'custom-header', $args );  

允许上传新图像:

  1. $args = array(   
  2.     'width'         => 980,   
  3.     'height'        => 60,   
  4.     'default-image' => get_template_directory_uri() . '/images/header.jpg',   
  5.     'uploads'       => true,   
  6. );   
  7. add_theme_support( 'custom-header', $args );  

输出一个大小可变的头部图像:

  1. $args = array(   
  2.     'flex-width'    => true,   
  3.     'width'         => 980,   
  4.     'flex-width'    => true,   
  5.     'height'        => 200,   
  6.     'default-image' => get_template_directory_uri() . '/images/header.jpg',   
  7. );   
  8. add_theme_support( 'custom-header', $args );  

后台的设置都做好了,前台调用设置选项的方法:

  1. <img src="<?php header_image(); ?>" height="<?php echo get_custom_header()->height; ?>" width="<?php echo get_custom_header()->width; ?>" alt="" />  

已有4条评论

fengk进行回复 取消回复