构建一个Bootstrap WordPress主题#10 设置侧边栏

现在我们设置侧边栏,以便我们可以添加侧边栏小部件

  1. 打开functions.php文件,在add_action()下面添加一个名为init_widgets()的函数,代码如下:
    // Widget Locations
    function init_widgets($id){
    register_sidebar(
    array('name' => 'Sidebar',
    'id' => 'sidebar',
    'before_widget' => '<div class="panel panel-default">',
    'before_title' => '<div class="panel-heading"><h3 class="panel-title">',
    'after_title' => '</h3></div><div class="panel-body">'
    'after_widget' => '</div></div>',));
    }

它的作用是允许我们在窗口小部件呈现之前插入代码,我们希望我们的小部件在Bootstrap panel内渲染,所以我们使用了带有Bootstrap的 panel 和 panel-default类的 div。

2. 在下方添加一个add_action( ) 函数,放入钩子widgets_init,和要执行的函数init_widgets,保存代码

add_action('widgets_init', 'init_widgets');

3. 到站点后台刷新页面,外观功能下出现了小工具选项和Sidebar显示位置

4. 把“目录分类”,“近期文章”和“近期评论”工具拖到sidebar中

5. 刷新前端页面

现在我们遇到了一个问题,因为我希望类别显示为列表组,格式比这更好。所以我们需要做的是在 theme 文件夹内部创建自己的这些小部件版本。

6. 我们在自己的主题文件夹内创建另一个名为widgets的文件夹。通过文件管理器我们先转到wp-includes |widgets文件夹,你会看到所有的核心小工具文件:

找到文章分类,最近评论和最近帖子小工具文件,然后复制(确保不剪切)并将它们粘贴到我们新创建的widgets文件夹中。

7. 先从文章分类开始,在编辑器中打开它,修改类名:

class WP_Widget_Categories_Custom extends WP_Widget {

(同理,也要修改另外两个小工具中的类名,同样是在原类名后面加上 _Custom)

接下来修改其中的 ul 和 li 标签,为它添加一个Bootstrap类:

<ul class="list-group">

还需要为列表项添加 list-group-item 类,这样做之前我们需要先在functions.php文件中进行一些自定义,否则无法从内部访问列表项标签,打开functions.php文件,到代码最下方,添加如下代码:

// Adds 'list-group-item' to categories li
function add_new_class_list_categories($list){
$list = str_replace('cat-item', 
'cat-item list-group-item', $list);
return $list;
}
add_filter('wp_list_categories', 'add_new_class_list_categories');

接下来还需要注册这些小工具才能使用它们,很简单只需要在functions.php文件顶部引入它们,我们使用require_once()引入这些小工具:

require_once('widgets/class-wp-widget-categories.php');
require_once('widgets/class-wp-widget-recent-posts.php');
require_once('widgets/class-wp-widget-recent-comments.php');

现在我们开始注册它们,在代码最底部,我们将添加函数wordstrap_register_widgets()。 同样我们需要类名,我们添加’WP_Widget_Recent_Posts_Custom’。 下一个将是‘WP_Widget_Recent_Comments_Custom’。 最后一个是添加‘WP_Widget_Categories_Custom’:


//Register Widgets
function wordstrap_register_widgets(){
register_widget('WP_Widget_Recent_Posts_Custom');
register_widget('WP_Widget_Recent_Comments_Custom');
register_widget('WP_Widget_Categories_Custom'); 
}

9. 现在我们添加另一个add_action(),再次使用’widgets_init’钩子,接着写入函数名称
‘wordstrap_register_widgets’:

add_action('widgets_init', 'wordstrap_register_widgets');

保存代码,返回前端页面刷新:

10. 我们希望所有的小工具都应用list-group和list-group-item样式。我们转到class-wp-widget-recent-posts.php小部件文件,找到< ul >标签。 我们添加一个list-group类,然后< li >标签将有一个list-group-item类:

<ul class="list-group">
<?php foreach ( $r->posts as $recent_post ) : ?>
<li class="list-group-item">

刷新前端页面

再打开class-wp-widget-recent-comments.php文件,找到ul和li标签,同样为他们添加样式:

$output .= '<ul class="list-group" id="recentcomments">';
if(is_array($comments) && $comments){
$post_ids = array_unique(wp_list_pluck($comments, 'comment_post_ID'));
_prime_post_caches($post_ids, 
strpos(get_option('permalink_structure'), '%category'),
 false);
foreach((array) $comments as $comment){$output .= '<li class="list-group-item recentcomments">';

保存代码,重载前端页面

现在我们统一了自定义小工具的样式,最重要的是这些自定义小工具只会在这个主题加载时生效,不会影响到WordPress的核心代码