WordPress 3.0给我们带来了以往版本所没有的一些明显的新特性,其中一个显著的亮点就是让我们可以在后台控制面板中设定主题的头部图像,这个功能同时也被整合到了其默认主题 Twenty Ten中。到了WordPress3.2,在新的默认主题Twenty Eleven中,我们还可以设定随机显示头部图像。这篇文章将要讲述的是,如果你使用的并不是WordPress的默认主题,你的主题又不支持自定义头部图像(Custom Header),但你又很需要这个功能,那怎么办呢?下文所介绍的方法可以帮你轻松实现你的这个愿望。
添加代码到Functions.php文件
你的主题中应该有一个功能函数文件functions.php,如果没有则自建一个,然后复制并粘贴以下代码进去:
- <?php
- //Check see if the customisetheme_setup exists
- if ( !function_exists('customisetheme_setup') ):
- //Any theme customisations contained in this function
- function customisetheme_setup() {
- //Define default header p_w_picpath
- define( 'HEADER_IMAGE', '%s/header/default.jpg' );
- //Define the width and height of our header p_w_picpath
- define( 'HEADER_IMAGE_WIDTH', apply_filters( 'customisetheme_header_p_w_picpath_width',960 ) );
- define( 'HEADER_IMAGE_HEIGHT', apply_filters( 'customisetheme_header_p_w_picpath_height',220 ) );
- //Turn off text inside the header p_w_picpath
- define( 'NO_HEADER_TEXT', true );
- //Don't forget this, it adds the functionality to the admin menu
- add_custom_p_w_picpath_header( '', 'customisetheme_admin_header_style' );
- //Set some custom header p_w_picpaths, add as many as you like
- //%s is a placeholder for your theme directory
- $customHeaders = array (
- //Image 1
- 'perfectbeach' => array (
- 'url' => '%s/header/default.jpg',
- 'thumbnail_url' => '%s/header/thumbnails/pb-thumbnail.jpg',
- 'description' => __( 'Perfect Beach', 'customisetheme' )
- ),
- //Image 2
- 'tiger' => array (
- 'url' => '%s/header/tiger.jpg',
- 'thumbnail_url' => '%s/header/thumbnails/tiger-thumbnail.jpg',
- 'description' => __( 'Tiger', 'customisetheme' )
- ),
- //Image 3
- 'lunar' => array (
- 'url' => '%s/header/lunar.jpg',
- 'thumbnail_url' => '%s/header/thumbnails/lunar-thumbnail.jpg',
- 'description' => __( 'Lunar', 'customisetheme' )
- )
- );
- //Register the p_w_picpaths with WordPress
- register_default_headers($customHeaders);
- }
- endif;
- if ( ! function_exists( 'customisetheme_admin_header_style' ) ) :
- //Function fired and inline styles added to the admin panel
- //Customise as required
- function customisetheme_admin_header_style() {
- ?>
- <style type="text/css">
- #wpbody-content #headimg {
- height: <?php echo header_p_w_picpath_height; ?>px;
- width: <?php echo header_p_w_picpath_width; ?>px;
- border: 1px solid #333;
- }
- </style>
- <?php
- }
- endif;
- //Execute our custom theme functionality
- add_action( 'after_setup_theme', 'customisetheme_setup' );
- ?>
说明:上面代码中设定了三张头部图像,分别为default.jpg、tiger.jpg、lunar.jpg以及它们各自的缩略图(分别为tiger-thumbnail.jpg、pb-thumbnail.jpg、lunar-thumbnail.jpg)。你可以自己制作这些图像(包括它们相应的缩略图),然后将这些图像都放到主题目录下面的header文件夹(没有则自建一个)中。其中,
Default.jpg 是默认显示的头部图像,名称可自定
960 是默认头部图像的宽度
220 是默认头部图像的高度
保存好上面的代码之后,你会在WordPress后台的左侧【外观】菜单下看到一个叫做【顶部】的子菜单,就跟WordPress默认主题Twenty Ten或Twenty Eleven一样,打开【顶部】菜单即可看到如下图所示的自定义顶部图像选项页面:
![Add a Customizable Header p_w_picpath to your WordPress Theme Add a Customizable Header p_w_picpath to your WordPress Theme](http://www.software8.co/uploads/allimg/130119/0U0443Z3-0.png)
进入自定义顶部图像选项页面之后,你可以自己再上传一张图片作为固定的主题头部图像,也可以从预先制作好的图像中(默认图像一栏)选择一张,或者选择随机显示也可以。
让头部图像在你的主题中显示出来
这是最后一步,你还得在主题的header.php文件中适当的位置插入以下代码,这样头部图像才能正常显示出来 :
- <div id="header">
- <!-- other header code here.... -->
- <!-- This line adds the header to the theme -->
- <img id="headerimg" src="<?php header_p_w_picpath(); ?>" width="<?php echo HEADER_IMAGE_WIDTH; ?>" height="<?php echo HEADER_IMAGE_HEIGHT; ?>" alt="Header p_w_picpath alt text" />
- </div>
一般情况下,你找到id 为header的那对<div>···</div> 标签,将代码放到里面即可。
原文链接:http://www.software8.co/cms/wordpress/2764.html
原文链接:http://www.software8.co/cms/wordpress/2764.html
转载于:https://blog.51cto.com/6339835/1122157