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' ); |
在模板的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
【推荐】博客园的心动:当一群程序员决定开源共建一个真诚相亲平台
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】Flutter适配HarmonyOS 5知识地图,实战解析+高频避坑指南
【推荐】开源 Linux 服务器运维管理面板 1Panel V2 版本正式发布
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· MySQL索引完全指南:让你的查询速度飞起来
· 一个字符串替换引发的性能血案:正则回溯与救赎之路
· 为什么说方法的参数最好不要超过4个?
· C#.Net 筑基-优雅 LINQ 的查询艺术
· 一个自认为理想主义者的程序员,写了5年公众号、博客的初衷
· MySQL索引完全指南:让你的查询速度飞起来
· 本地搭建一个对嘴AI工具
· 我用这13个工具,让开发效率提升了5倍!
· 总结下参与以及看到的一些好的业务设计的 pattern
· 商品中心—15.库存分桶扣减的技术文档