wordpress自定义头部banner


打开主题页面functions.php添加以下代码。
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/********************Custom Header***************************************************/
 
//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 image
        define( 'HEADER_IMAGE', '%s/images/banner.jpg' );
        //Define the width and height of our header image
        define( 'HEADER_IMAGE_WIDTH', apply_filters( 'customisetheme_header_image_width', 980 ) );
        define( 'HEADER_IMAGE_HEIGHT', apply_filters( 'customisetheme_header_image_height', 122 ) );
 
        //Turn off text inside the header image
        define( 'NO_HEADER_TEXT', true );
 
        //Don't forget this, it adds the functionality to the admin menu
        add_custom_image_header( 'customisetheme_header_style', 'customisetheme_admin_header_style' );
 
        //Set some custom header images, add as many as you like
        //%s is a placeholder for your theme directory
        $customHeaders = array (
                //Image 1
                'perfectbeach' => array (
                'url' => '%s/images/banner.jpg',
                'thumbnail_url' => '%s/images/banner-small.jpg',
                'description' => __( 'Perfect Beach', 'customisetheme' )
            ),
                //Image 2
                'tiger' => array (
                'url' => '%s/images/banner5.jpg',
                'thumbnail_url' => '%s/images/banner5-small.jpg',
                'description' => __( 'Tiger', 'customisetheme' )
            )
        );
        //Register the images with WordPress
        register_default_headers($customHeaders);
    }
endif;
 
if ( ! function_exists( 'customisetheme_header_style' ) ) :
    //Function fired and inline styles added to the admin panel
    //Customise as required
    function customisetheme_header_style() {
    ?>
        <style type="text/css">
        #header {
            background: url(<?php header_image(); ?>);
        }
    </style>
    <?php
    }
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_IMAGE_HEIGHT; ?>px;
                width: <?php echo HEADER_IMAGE_WIDTH; ?>px;
                border: 1px solid #333;
            }
        </style>
    <?php
    }
endif;
 
//Execute our custom theme functionality
add_action( 'after_setup_theme', 'customisetheme_setup' );
另外将banner.jpg和banner5.jpg及其缩略图banner-small.jpg和banner5-small.jpg放在主题images目录下。
在模板的header.php页面中,有以下代码:

 <div id="header">
    
这里是你的页面头部的文字和图片
</div>


然后,就可以在后台管理--【外观】--【顶部】--选择banner图片。

参考资料:
http://wplift.com/add-a-customisable-header-image-to-your-wordpress-theme http://codex.wordpress.org/Custom_Headers

posted on 2012-03-08 16:27  IT技术畅销书  阅读(1142)  评论(0)    收藏  举报

编辑推荐:
· MySQL索引完全指南:让你的查询速度飞起来
· 一个字符串替换引发的性能血案:正则回溯与救赎之路
· 为什么说方法的参数最好不要超过4个?
· C#.Net 筑基-优雅 LINQ 的查询艺术
· 一个自认为理想主义者的程序员,写了5年公众号、博客的初衷
阅读排行:
· MySQL索引完全指南:让你的查询速度飞起来
· 本地搭建一个对嘴AI工具
· 我用这13个工具,让开发效率提升了5倍!
· 总结下参与以及看到的一些好的业务设计的 pattern
· 商品中心—15.库存分桶扣减的技术文档

导航

< 2012年3月 >
26 27 28 29 1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
1 2 3 4 5 6 7

统计

点击右上角即可分享
微信分享提示