by
post_content)), 0, 260,"..."); ?>
```
# 四、创建single.php页
WordPress中,每篇文章的展示需要single.php,如果没有的话,系统会默认调用index.php,我们首先编写single.php的内容,以它为框架在细化每一个部分。singe.php同样用到了The Loop,因此可以将index.php复制一份来修改。
4.1、网页布局代码
将所有的内容放入一个container中居中,然后通过bootstrap的栅格系统分为左9右3的布局。右三用来输出sidebar的内容,代码如下:
```
```
4.2、加入函数命令,代码如下
```
```
# 五、编辑sidebar.php侧边栏
侧边栏的功能非常强大,扩展性定制性都非常的高,很多小工具都要放到这里,今天我们只是做一个初级入门教程,放入常用的分类目录、最新文章、标签云、文章存档等功能。
1、静态布局
```
分类目录
最新文章
标签云
......
文章存档
```
2、加入功能判断函数代码如下:
```
分类目录
最新文章
标签云
文章存档
```
3、替换以下页面的边栏静态代码
在index.php、archive.php、page.php和single.php页面的边栏代码处改为:
```
```
4、让我们的主题支持侧边栏小工具挂件
目前我们的主题还不支持在WordPress后台 – 外观 – 小工具,可以正常地拖动小工具到侧边栏的,无法让更多的小工具放到我们的边栏,我们需要在functions.php里注册我们的边栏小工具,想要了解更多功能建议你阅读 WordPress 函数register_sidebar()[wordpress-register_sidebar.html](http://www.shanhubei.com/wordpress-register_sidebar.html)
好了,打开functions.php复制以下代码我们就可以使用WordPress后台 – 外观 – 小工具了,这样让我们的侧边栏更加强大
```
if( function_exists('register_sidebar') ) {
register_sidebar(array(
'name' => 'First_sidebar',
'id' => 'sidebar-1',
'before_widget' => '',
'after_widget' => '',
'before_title' => '
',
'after_title' => '
'
));
}
```
# 六、编辑comments.php
在文章的页面single.php要显示评论的话,可以使用`comments_template();`函数调用评论的模板,评论功能是和读者交互的重要途径,今天我们来实现主题的评论功能。
1、在single.php文章内容下面要显示评论的地方加入以下代码,通过`comments_template();`函数调用评论页面comments.php。
```
```
2、在我们的主题根目录下建立评论模板comments.php,设计评论样式。
```
```
3、加入代码
```
```
get_comments_number() 获得评论的数量
get_the_title() 获得标题
wp_list_comments() 获取评论内容列表
4、使用自定义bootstrap评论列表
修改如下,首先我们要自定义评论列表,其中bootstrapwp_comment自定义的回调函数。建议你必须阅读 wp_list_comments()使用回调函数自定义评论展示方式
```
'bootstrapwp_comment',
) ); ?>
```
在functions.php添加自定义回调函数bootstrapwp_comment,函数方法如下:
```
/**
* 评论列表的显示
*/
if ( ! function_exists( 'bootstrapwp_comment' ) ) :
function bootstrapwp_comment( $comment, $args, $depth ) {
$GLOBALS['comment'] = $comment;
switch ( $comment->comment_type ) :
case 'pingback' :
case 'trackback' :
// 用不同于其它评论的方式显示 trackbacks 。
?>
id="comment-">
', '' ); ?>
id="li-comment-">
'
',
// 评论之前的提示内容
'comment_notes_before' => ' ',
// 评论之后的提示内容
'comment_notes_after' => ' ',
// 默认的字段,用户未登录时显示的评论字段
'fields' => apply_filters( 'comment_form_default_fields', array(
// 作者名称字段
'author' => ' ' . ( $req ? '*' : '' ) .
'' .
''.
'
',
// 电子邮件字段
'email' => ' ' . ( $req ? '*' : '' ) .
'' .
'' .
'
',
// 网站地址字段
'url' => '' . ( $req ? '*' : '' ) .
'' .
'
' )
) );
?>
```
# 七、archive.php文件
archive.php文件 又称文章归档页面。在WordPress中,文章归档页面是一个非常重要的页面,特别是当你的wordpress网站文章很多的时候,它将文章以年月日的分类方式对文章进行归类,可以让读者很方便的迅速找到某年某日的文章。现在,很多博客,比如腾讯微博,QQ空间都有时间轴的显示方式,今天我们用也用时间轴的形式来记录显示我们的wordpress网站文章归档页面,并且加入伸缩功能。
1、在我们的主题更目录下建立archive.php(文章归档页面)
输入以下代码:
```
post_date);
$month = mysql2date('n', $post->post_date);
$day = mysql2date('j', $post->post_date);
if($year != $previous_year || $month != $previous_month) :
if($ul_open == true) :
echo '';
endif;
echo '
'; echo the_time('Y-m'); echo '
';
echo '
';
$ul_open = true;
endif;
$previous_year = $year; $previous_month = $month;
?>
-
```
由于主题是采用了Bootstrap框架所以加了,
```
```
根据你自己的主题,可以选择适当的布局。
2、打开主题根目录下的style.css文件,加入以下css代码
```
/*
* 文章归档页面样式
*/
.archive-title{border-bottom:1px #eee solid;position:relative;padding-bottom:4px;margin-bottom:10px}
.archives li{list-style-type:none}
.archives li a{padding:8px 0;display:block}
.archives li a:hover .atitle:after{background:#ff5c43}
.archives li a span{display: inline-block;width:100px;font-size:12px;text-indent:20px}
.archives li a .atitle{display: inline-block;padding:0 15px;position:relative}
.archives li a .atitle:after{position:absolute;left:-6px;background:#ccc;height:8px;width:8px;border-radius:6px;top:8px;content:""}
.archives li a .atitle:before{position:absolute;left:-8px;background:#fff;height:12px;width:12px;border-radius:6px;top:6px;content:""}
.archives{position:relative;padding:10px 0}
.archives:before{height:100%;width:4px;background:#eee;position:absolute;left:130px;content:"";top:0}
.m-title{position:relative;margin:10px 0;cursor:pointer}
.m-title:hover:after{background:#ff5c43}
.m-title:before{position:absolute;left:127px; background:#fff; height:18px;width:18px;border-radius:6px;top:3px;content:""}
.m-title:after{position:absolute;left:127px;background:#ccc;height:12px;width:12px;border-radius:6px;top:6px;content:""}
```
如果你引入以上CSS显示效果错乱的话,那么你就得调整上面的css布局了。
3、点击年月实现伸缩功能的话需要引入以下js
```
$('.archives ul.archives-monthlisting').hide();
$('.archives ul.archives-monthlisting:first').show();
$('.archives .m-title').click(function() {
$(this).next().slideToggle('fast');
return false;
});
```
---end---
评论标题
评论内容列表