八、制作侧边栏sidebar.php
get_sidebar()函数是可以加参数的。 比如:
这个代码加载sidebar-1.php,有的人希望网站上的首页、内页、分类页等各个页面的侧边栏不一样,这样就需要有多个侧边栏,这时候就得给这个函数加参数了。
编辑sidebar.php文件,将里面的代码删除,因为那些都是静态的,我们需要从后台设置小工具,所以删除了,改成:
class=
"grid_4" >
if(!function_exists('dynamic_sidebar')
||!dynamic_sidebar('First_sidebar')):?>
分类目录
endif;?>
if(!function_exists('dynamic_sidebar')
||!dynamic_sidebar('Second_sidebar')):?>
最新文章
endif;?>
if(!function_exists('dynamic_sidebar')
||!dynamic_sidebar('Third_sidebar')):?>
标签云
endif;?>
if(!function_exists('dynamic_sidebar')
||!dynamic_sidebar('Fourth_sidebar')):?>
文章存档
endif;?>
class="hrgrid_12clearfix" >
仅仅有代码是不够的,还需要函数支持,在functions.php里面添加代码:
if (function_exists('register_sidebar')){
register_sidebar(array (
'name'=>'首页侧边栏',
'before_widget'=>'"%1$s"class ="sidebar_li%2$s" >',
'after_widget'=>'',
'before_title'=>'
',
'after_title'=>'',
));
}
通过添加函数注册一个侧边栏,添加了这个函数,你的主题就支持侧边栏功能了,在后台多了小工具页面,且能看到有侧边栏选项。
九、制作index.php文件
又一篇文章循环出多篇且改为动态:例如:
class=
"post" >
class="title" >"single.html">文章标题
class="sub" >"#">标签1,"#">标签12•发布时间•"#">评论数
class="hrdottedclearfix" >
class="thumb" alt="" src="/p_w_picpaths/610x150.gif" />
文章内容
class="clearfix" >"single.html"class ="buttonright" >阅读全文按钮
class="hrclearfix" >
1、文章标题
将文章标题代码:
class="title" >"single.html">Loreumipsiummassacrasphasellus
替换成
class="title" >""rel="bookmark" >
php函数:the_permalink();是输出当前文章的链接地址,注意是直接输出;the_title();函数直接输出当前文章的标题。
2、文章标签
将index.php里面的标签代码:
替换成:
the_tags函数直接输出文章标签。
3、日期
找到日期文字,31stSep,09直接替换成:
the_time函数直接输出文章日期,至于输出格式Y年n月j日可以改你也可以改成Y-n-j这些参数非常多,请自己到官网查询。
4、评论数 在文章归档页显示文章的评论数和点击数似乎很是流行,将里面的评论代码
替换成
comments_popup_link()函数里面的三个参数分别代表输出无评论、一条评论、N条评论,里面那个%相当于占位符了。这个函数输出的代码带有链接,会链接到文章页,并定位到评论位置。
5、文章内容
在文章内容的位置添加代码
即可,事实上我们要输出的是摘要,而the_content是输出文章内容的,但是在首页和归档页,如果你在文章中添加了more标签,则会输出more标签之前的内容,并且在后面加上一个“阅读全文”的链接。
但是很多人会想到使用另一个输出摘要的函数the_excerpt();不建议这样做,这个函数会输出文章的摘要(也就是在后台添加文章的时候有一个专门用来添加摘要的地方),如果没有摘要的话,就会自动截取前50个字符,不过这是对于英文而言,对于中文的多字节语言, 这个函数是截取不了的,所以他会全文输出,相比而言,添加More标签更麻烦还是填写摘要更麻烦呢?不过如果打算每篇文章手动指定一个摘要的话,建议你使用the_excerpt函数。
6、文章循环
前面的代码我们已经将一篇文章的框架写好了,现在要做的就是将这个文章框架代码放在一个循环语句中输出。
在文章框架的前面,也就是有注释--BlogPost-->的地方,添加代码,效果:
if(have_posts()):while (have_posts()):the_post();?>
再在文章框架后面,添加结束循环的代码,找到:
修改为
class="hrclearfix" >
endwhile;?>
再找到:
修改为:
else:?>
class="title" >"#"rel="bookmark" >未找到
没有找到任何文章!
endif;?>
?phpget_sidebar();?>
OK,到此为止我们的循环代码已经完成,分析一下我们刚才添加的代码,大致是这样子的:
if(have_posts()):while (have_posts()):the_post();?>
文章html骨架
endwhile;?>
else:?>
输出找不到文章提示
endif;?>
have_posts()函数是判断当前是否有文章:当前页面要输出的所有文章存放在一个全局数组$posts中,have_post()函数就是检查这个数组的一个计数器,如果当前还有文章,那么就返回true,如果没有就返回false; the_post()函数用来将have_posts计数器前移,并且将当前文章填进变量$post中,而前面的函数the_title(),the_content()这些函数只是用用来输出$post变量中的的内容,你完全可以用
来代替the_title()函数,你也可以输出$post变量中的其它内容,比如文章ID。
7、文章分页 前面的代码一次只能输出部分文章,如果整个博客有100篇文章,不可能将100篇文章全部列出来,这时候就需要分页显示了。 找到我们的分页代码:
class="clearfix" >"#"class ="buttonfloat" ><<PreviousPosts"#"class ="buttonfloatright" >NewerPosts>>
替换成
class="clearfix" >class ="floatright" >
十、制作文章单页模板single.php
1、文章标题 找到文章标题:
class="title" >"single.html">Loreumipsiummassacrasphasellus
还记得我们上篇教程讲的获取文章链接、标题的代码吗?
2、文章标签:
改成
3、日期 将日期改为:
4、评论数
改成
5、文章内容。 先将文章的图片删了,删除下面的代码:
class="thumb" src="/p_w_picpaths/610x150.gif" alt="" />
然后将所有文章内容,即:和之间的代码全部删除,替换成:
6、评论和返回首页
class="clearfix" >"blog.html"class ="buttonfloat" ><<BacktoBlog"#commentform"class ="buttonfloatright" >Discussthispost
改成:
class="clearfix" >""class ="buttonfloat" ><<返回首页"#commentform"class ="buttonfloatright" >发表评论
好了,前面说过文章单页的内容页需要放在一个循环语句中(事实上是我们需要在输出文章的前面执行the_post()函数,这个函数会生成文章变量$post)。 在的后面添加代码,效果:
if(have_posts()):the_post();update_post_caches($posts );?>
然后在代码
的前面,注意咯。“的前面”添加代码,完成效果:
else:?>
endif;?>
这里的操作跟首页差不多,不过这里只需要输出一篇文章,所以while添加与否没有多大关系,需要提醒的是,一定要记住添加了if,就得有endif,添加了while,就得有endwhile。 可能其他语法不会这样用,其实这里你也可以改成用{}的。比如:
if(have_posts()){the_post();?>
OK,文章单页制作方法就完成了。
十一、评论模板comments.php
例如有:
Comments
class="hrdottedclearfix" >
class="commentlist" >
class="comment" >
class=
"comment_content" >
class=
"clearfix" >
class="author_name" >"">JoeBloggs
class="comment-metacommentmetadata" >January6,2010at6:26am
class=
"comment_text" >
Donecleo.Aliquamrisuselit,luctusvel,interdumvitae,malesuadaeget,elit.Nullavitaeipsum.Donecligulaante,bibendumsitamet,elementumquis,viverraeu,ante.Fuscetincidunt.Maurispellentesque,arcuegetfeugiataccumsan,ipsummimolestieorci,utpulvinarsapienloremnecdui.
class="hrclearfix" >
"comment_form"action="" method="post" >
Addacomment
class="hrdottedclearfix" >
class="clearfix" >
for="name" >YourName
"name"name="name" type="text" />
class="clearfix" >
for="email" >YourEmail
"email"name="email" type="text" />
class="clearfix" >
for="email" >YourWebsite
"website"name="website" type="text" />
class="clearfix" >
for="message" >Comment
"message"name="message" rows="3" cols="40" >
class="clearfix" >
"submit"class ="buttonmediumblackright" >Addcomment
在single.php中添加评论模块用下面这句:
comments_template()函数默认的就是加载主题文件夹下面的comments.php文件,这个函数也是可以带参数的,以便让你可以加载别的文件,比如某些页面你需要加载一个不一样的评论表单,你就需要使用comments_template()带上参数
为了防止某些恶意用户直接打开评论文件,我们在comments.php的头部添加代码:
if (isset($_SERVER ['SCRIPT_FILENAME'])&&'comments.php'==basename ($_SERVER ['SCRIPT_FILENAME']))
die ('Pleasedo notloadthispagedirectly.Thanks!');
?>
修改评论列表:
wordpress有自动输出评论列表的函数wp_list_comments(),所以我们将原来的评论列表代码删除,换上这个函数,但是我们还需要加一些判断功能,比如评论需要密码才能查看、评论已经关闭、还没有评论这几个情况都要有不同的输出,所以将原来的评论代码:
class="comment" >
class=
"comment_content" >
class=
"clearfix" >
class="author_name" >"">JoeBloggs
class="comment-metacommentmetadata" >January6,2010at6:26am
class=
"comment_text" >
Donecleo.Aliquamrisuselit,luctusvel,interdumvitae,malesuadaeget,elit.Nullavitaeipsum.Donecligulaante,bibendumsitamet,elementumquis,viverraeu,ante.Fuscetincidunt.Maurispellentesque,arcuegetfeugiataccumsan,ipsummimolestieorci,utpulvinarsapienloremnecdui.
替换成:
if (!empty empty ($post ->post_password)&&$_COOKIE ['wp-postpass_'.COOKIEHASH]!=$post ->post_password){
?>
class="decmt-box" >
"#addcomment">请输入密码再查看评论内容.
}else if (!comments_open()){
?>
class="decmt-box" >
"#addcomment">评论功能已经关闭!
}else if (!have_comments()){
?>
class="decmt-box" >
"#addcomment">还没有任何评论,你来说两句吧
}else {
wp_list_comments('type=comment&callback=aurelius_comment');
}
?>
上面的wp_list_comments函数中的两个参数,其中type=comment意思只输出评论,除了评论还有pings\trackback\pingback等等什么的,callback=aurelius_comment意思是调用一个自定义的函数函数aurelius_comment来显示评论。 自定义的函数我们需要添加在主题的functions.php文件中,所以请在functions.php中的“?>”前面加上下面的代码,如果functions.php文件中已经存在了下面的代码,就不要再添加了:
function aurelius_comment($comment ,$args ,$depth )
{
$GLOBALS ['comment']=$comment ;?>
class="comment" id="li-comment-" >
class=
"gravatar" >if(function_exists('get_avatar')&&get_option('show_avatars')){
echo get_avatar(
$comment ,48);}?>
array_merge($args ,array ('reply_text'=>'回复','depth'=>$depth ,'max_depth'=>$args ['max_depth'])))?>
class=
"comment_content" id=
"comment-" >
class=
"clearfix" >
class="author_name" >%s'),get_comment_author_link());?>
class="comment-metacommentmetadata" >发表于:echoget_comment_time('Y-m-dH:i');?>
class=
"comment_text" >
if($comment ->comment_approved=='0'):?>
你的评论正在审核,稍后会显示出来!
endif;?>
上面的自定义函数中用到的几个函数的说明如下:
get_avatar($id_or_email ,$size ,$default ,$alt );
?>
get_comment_author_link();
?>
get_comment_time();
edit_comment_link();
comment_text();
?>
添加了上面的代码评论已经能正确显示了,接下来添加提交评论的表单。
将原来comments.php中的评论表单代码删除:
"comment_form"action="" method="post" >
Addacomment
class="hrdottedclearfix" >
class="clearfix" >
for="name" >YourName
"name"name="name" type="text" />
class="clearfix" >
for="email" >YourEmail
"email"name="email" type="text" />
class="clearfix" >
for="email" >YourWebsite
"website"name="website" type="text" />
class="clearfix" >
for="message" >Comment
"message"name="message" rows="3" cols="40" >
class="clearfix" >
"submit"class ="buttonmediumblackright" >Addcomment
实际上你不需要再手动输入每个表单项了,新版的wordprss提供了一个非常方便的函数:comment_form(),添加代码如下:
if(comments_open()):?>
if(get_option('comment_registration')&&!$user_ID ):?>
"%s">登录才能发表评论.'),get_option('siteurl')."/wp-login.php?redirect_to=" .urlencode(get_permalink()));?>
else:?>
$defaults=array (
'comment_notes_before'=>'' ,
'label_submit'=>__('提交评论'),
'comment_notes_after'=>''
);
comment_form($defaults );
endif ;
else :?>
endif;?>
可以看到上面的代码中也添加了判断,看是否允许评论,是否需要登录才能评论。 你完全可以通过comment_form()函数的各个参数再配合css输出一个个性化的表单
十二、单页面模板page.php
文章又可以分为:普通文章、独立页面。在wordpress后台已经将文章和内容区分开了,实际上文章和页面只是两种文章形式,它们在数据库中存储在同一个表中,存储方式也是一样,只有一个属性不同:post,page。所以page的添加方式,显示方式跟posts一样,他们的模板代码页基本相似
我们在添加页面的时候,右边有个选项页面属性:
可以看到有个页面模板选项,一般只有默认模板,也就是我们的page.php,如果想添加另外的模板,比如你将page.php复制一份,然后重命名为page-tep.php,
编辑page-tep.php在最最最前面加上
再进入后台编辑页面,就能看到有一个留言板选项了。