编写PHP脚本使WordPress的主题支持Widget侧边栏

yipeiwu_com7年前PHP代码库

帮网友小改了一下主题. 任务比较简单, 只是为一个三栏主题添加对 Widget 的支持而已,就先从这次简单的案例开始说吧.

20151214152214148.png (600×360)

单侧边栏

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

相关文章

解析关于java,php以及html的所有文件编码与乱码的处理方法汇总

php文件中在乱码(如a.php文件在浏览器乱码):header("Content-Type:text/html;charset=utf-8")是设置网页的。mysql_query("s...

php利用反射实现插件机制的方法

本文实例讲述了php利用反射实现插件机制的方法。分享给大家供大家参考。具体实现方法如下: 复制代码 代码如下:<?php /**  * @name &n...

php上传图片生成缩略图(GD库)

首先来一段简单的php上传图片生成缩略图的详细代码,分享给大家供大家参考,具体内容如下 <?php function createThumbnail($imageDir...

据说是雅虎的一份PHP面试题附答案

从网上搜集到的,据说是雅虎的面试题。 1. Which of the following will not add john to the users array? 1. $users[...

PHP5.3的垃圾回收机制(动态存储分配方案)深入理解

垃圾回收机制是一种动态存储分配方案。它会自动释放程序不再需要的已分配的内存块。 自动回收内存的过程叫垃圾收集。垃圾回收机制可以让程序员不必过分关心程序内存分配,从而将更多的精力投入到业务...