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

yipeiwu_com6年前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

相关文章

php socket方式提交的post详解

<?  /*  ** POST报文到主机  */  function PostToHost($url, $da...

php通过COM类调用组件的实现代码

在PHP 4.2.0 至 4.2.3中,可以使用w32api_register_function 函数调用外部的DLL,前提是需要在php.ini中打开扩展的php_w32api.dll...

The specified CGI application misbehaved by not returning a complete set of HTTP headers

是错误报告: The specified CGI application misbehaved by not returning a complete set of HTTP heade...

5种PHP创建数组的实例代码分享

看这篇文章之前相信大家都已经看过PHP中文手册关于数组这一节的讲解了,怎么样呢,看懂了多少?至少我第一次阅读文档时是一头雾水,也许是因为在翻译的不够通俗易懂吧^_^!!这里UncleTo...

Lumen timezone 时区设置方法(慢了8个小时)

根据 Laravel 4.x 和 5.0 的经验, 只需要到 config/app.php 中设置下 ‘timezone' 参数为 ‘PRC' 就好了, 找到 Lumen 的 confi...