编写PHP脚本使WordPress的主题支持Widget侧边栏
作者:goldensun 时间:2023-11-23 20:27:13
帮网友小改了一下主题. 任务比较简单, 只是为一个三栏主题添加对 Widget 的支持而已,就先从这次简单的案例开始说吧.
单侧边栏
functions.php
<?php
if( function_exists('register_sidebar') ) {
register_sidebar(array(
'before_widget' => '<li class="widget">', // widget 的开始标签
'after_widget' => '</li>', // widget 的结束标签
'before_title' => '<h3>', // 标题的开始标签
'after_title' => '</h3>' // 标题的结束标签
));
}
?>
sidebar.php
<div id="sidebar">
<ul class="widgets">
<?php // 如果没有使用 Widget 才显示以下内容, 否则会显示 Widget 定义的内容
if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar() ) :
?>
<!-- widget 1 -->
<li class="widget">
<h3>标题 1</h3>
<ul>
<li>条目 1.1</li>
<li>条目 1.2</li>
<li>条目 1.3</li>
</ul>
</li>
<!-- widget 2 -->
<li class="widget">
<h3>标题 2</h3>
<ul>
<li>条目 2.1</li>
<li>条目 2.2</li>
<li>条目 2.3</li>
</ul>
</li>
<?php endif; ?>
</ul>
</div>
双侧边栏
functions.php
<?php
if( function_exists('register_sidebar') ) {
register_sidebar(array(
'name' => 'Sidebar_1', // 侧边栏 1 的名称
'before_widget' => '<li class="widget">', // widget 的开始标签
'after_widget' => '</li>', // widget 的结束标签
'before_title' => '<h3>', // 标题的开始标签
'after_title' => '</h3>' // 标题的结束标签
));
register_sidebar(array(
'name' => 'Sidebar_2', // 侧边栏 2 的名称
'before_widget' => '<li class="widget">', // widget 的开始标签
'after_widget' => '</li>', // widget 的结束标签
'before_title' => '<h3>', // 标题的开始标签
'after_title' => '</h3>' // 标题的结束标签
));
}
?>
sidebar.php
<div id="sidebar_1">
<ul class="widgets">
<?php // 如果没有在侧边栏 1 中使用 Widget 才显示以下内容, 否则会显示 Widget 定义的内容
if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('sidebar_1') ) :
?>
<!-- widget 1 -->
<li class="widget">
<h3>标题 1</h3>
<ul>
<li>条目 1.1</li>
<li>条目 1.2</li>
<li>条目 1.3</li>
</ul>
</li>
<?php endif; ?>
</ul>
</div>
<div id="sidebar_2">
<ul class="widgets">
<?php // 如果没有在侧边栏 2 中使用 Widget 才显示以下内容, 否则会显示 Widget 定义的内容
if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('sidebar_2') ) :
?>
<!-- widget 2 -->
<li class="widget">
<h3>标题 2</h3>
<ul>
<li>条目 2.1</li>
<li>条目 2.2</li>
<li>条目 2.3</li>
</ul>
</li>
<?php endif; ?>
</ul>
</div>
N 侧边栏
请使用数学归纳法进行推理XD
标签:WordPress,侧边栏
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python脚本在Appium库上对移动应用实现自动化测试
2021-12-27 12:43:24
python随机模块random使用方法详解
2022-07-02 04:31:10
Python自动化构建工具scons使用入门笔记
2023-09-21 19:58:16
![](https://img.aspxhome.com/file/2023/8/134748_0s.png)
Django框架中表单的用法
2022-03-29 02:00:14
![](https://img.aspxhome.com/file/2023/0/103110_0s.gif)
Oracle Session每日统计功能实现
2023-07-22 09:47:24
![](https://img.aspxhome.com/file/2023/7/64087_0s.png)
详解MySQL中的SQRT函数的使用方法
2024-01-28 03:59:32
仿QQ和MSN消息提示的效果代码
2010-03-16 12:17:00
Python解决多进程间访问效率低的方法总结
2023-11-25 11:57:43
python画图中文不显示问题的解决方法
2023-05-30 14:07:09
![](https://img.aspxhome.com/file/2023/4/103694_0s.jpg)
CentOS 6.X系统下升级Python2.6到Python2.7 的方法
2023-01-05 03:36:18
python通过re正则表达式切割中英文的操作
2021-11-29 04:41:23
Python必知必会之os模块实例详解
2023-06-09 22:31:07
![](https://img.aspxhome.com/file/2023/6/99046_0s.jpg)
不需要用到正则的Python文本解析库parse
2022-11-08 17:28:09
![](https://img.aspxhome.com/file/2023/5/112405_0s.jpg)
详解Python 数据库 (sqlite3)应用
2024-01-21 06:14:46
![](https://img.aspxhome.com/file/2023/5/105275_0s.png)
Go语言通道之无缓冲通道
2024-04-25 15:25:57
![](https://img.aspxhome.com/file/2023/7/134107_0s.png)
SQL Server数据库重命名、数据导出的方法说明
2024-01-22 05:51:45
Python如何读写CSV文件
2023-03-23 08:41:13
对TensorFlow中的variables_to_restore函数详解
2022-09-11 00:49:19
php将ppt转jpg图片的具体步骤代码
2023-06-12 21:53:33
Python在cmd上打印彩色文字实现过程详解
2022-12-19 07:27:33
![](https://img.aspxhome.com/file/2023/8/90378_0s.png)