那么我们在开发 wordpress 主题的过程中,肯定会涉及到网站侧边栏工具的调用开发,网站侧边栏是非常实用的,目前大部分网站都有,比如添加最近文章,文章归档,最新评论,标签云,搜索框,类别,链接表等,最新访客,信息统计,广告 AD,订阅等等这里就不赘述了,这就用到了 dynamic_sidebar()函数用来支持自定义 sidebar 侧边栏,WordPress 默认情况下是没有小工具栏的。那么就看下我们分享的教程吧:
1.在 functions.php 文件添加以下代码
/** * register sidebar */ if (function_exists('register_sidebar')){ $sidebars = array( 'single' => '文章页侧栏', 'page' => '页面侧栏', ); foreach ($sidebars as $key => $value) { register_sidebar(array( 'name' => $value, 'id' => $key, 'before_widget' => '<div class="widget %2$s">', 'after_widget' => '</div>', 'before_title' => '<h3>', 'after_title' => '</h3>' )); }; }
这就注册了一个名称为文章边栏的侧边栏小工具,放在了 div 标签内,标题为 h3。
2.调用方法
<?php if (function_exists('dynamic_sidebar')){ if (is_single()){ dynamic_sidebar('single'); } else if (is_page() || is_home()){ dynamic_sidebar('page'); } } ?>
然后在后台-外观-小工具里面就可以设置 sidebar 自定义侧边栏了,增加或移除 widget 插件内容。看到上面的代码,感觉是不是很简单,其实 WordPress 也没有那么高大上,模块化很简单功能强大哦。
请一秒钟记住我们的网址:www.aliyuncms.com ! 转载请注明:WordPress开发教程:dynamic_sidebar()调用侧边栏 !