wordpress主题中的have_comments

get_the_category()
返回当前文章所属分类的数组集合
用法:&?php get_the_category( $id ) ?&
文章id,默认是当前文章ID,类型为整数,可选
返回的值:数组
eg1:如果需要使用返回的分类目录下的某个值,需要指定数组索引。可以这样理解,一篇文章可能属于多个分类目录,然后遍历返回结果可以获取每个分类的对象,再使用成员就即可获取想要的分类ID及分类名和其它内容。
$category = get_the_category();
//默认获取当前分类ID
echo $category[0]-&cat_
//使用$categories-&cat_name不能获得正确值,应该$categories[0]-&cat_name才能正确工作。
eg2:在文章页循环当前文章所属分类,输出图片以分类ID命名,且alt属性设置为分类名称。
foreach((get_the_category()) as $category) {
echo ‘&img src=”/images/’.$category-&cat_ID.’.jpg” alt=”‘.$category-&cat_name.'” /&';
eg3:通过当前分类id显示当前分类的所有的文章标题。
$cat=get_the_category();
$cat_id=$cat[0]-&cat_ID;
query_posts(‘order=asc&cat=’.$cat_id);
while (have_posts()):the_post();
&a href=”&?php the_permalink();?&”&&?php the_title();?&&/a&
&?php wp_reset_query(); ?&
eg4:在文章页通过指定的文章ID获取此文章所属分类的相关信息。如分类名称,分类别名,分类描述,分类所包含的文章数。
$categories = get_the_category($post-&ID);
//var_dump($categories);
echo $categories[0]-&cat_
echo $categories[0]-&category_
echo $categories[0]-&category_
echo $categories[0]-&category_
返回值各项含义:
category_nicename
一个slug,也就是系统根据分类名生成的url友好的名字
category_description
category_parent
category_count
该分类包含多少篇日志或者该分类被使用了多少次
函数是用来输出和获取用户信息的,如果这个函数被放在了外面,那么必须指定用户ID,而且显示的是当前文章作者的信息
如果元字段不存在,则不会打印
如果你不想打印数据,请使用
另外再加一点,一下方法已经不推荐使用,请使用当前函数代替
the_author_description()
the_author_link();
the_author_email()
the_author_url()
<?php the_author_meta( $field, $userID ); ?>
(string) 所需要显示的字段名称,有效值:
user_login
user_nicename
user_email
user_registered
user_activation_key
user_status
display_name
first_name
description
user_level
user_firstname
user_lastname
user_description
rich_editing
comment_shortcuts
admin_color
plugins_per_page
plugins_last_view
() (可选)如果用户ID字段被使用,那么这个功能显示了特定领域该用户ID。
Default: false
显示AIM的 screenname
This author’s AIM address is &?php the_author_meta(‘aim’); ?&
显示用户邮箱
The email address for user id 25 is &?php the_author_meta(‘user_email’,25); ?&
This author’s Twitter name is &?php the_author_meta(‘twitter’); ?&
将会输出:This author’s Twitter name is WordPress
the_author_meta() is located in .
WordPress的cos-html-cache插件为我们系统的静态化提供了非常好的解决方案,目前的版本是2.7.3。它可以将我们首页及文章(Post)进行静态化,使得访问速度大大提高,减少了服务器端的压力。不过对于页面、标签和分类,却似乎不太起作用,至少在我的服务器环境下(IIS7.0+FastCGI)是不行的。
为了能够实现它们,我分析了一下源代码,发现功能没有开启,但是可以实现的。具体修改方法及目的如下:
找到如下三行:
if( substr_count($_SERVER[’REQUEST_URI’], ‘.htm’) || ( SCRIPT_URI == CosSiteHome) ){
substr_count($_SERVER[’REQUEST_URI’], ‘../’))
$is_buffer =
if( !substr_count($buffer, ‘&!–cos-html-cache-safe-tag–&’) ) return
将他们分别做如下处理:
if( strpos($_SERVER[’REQUEST_URI’], ‘page’)==1 || strpos($_SERVER[’REQUEST_URI’], ‘tag’)==1 || strpos($_SERVER[’REQUEST_URI’], ‘category’)==1 || substr_count($_SERVER[’REQUEST_URI’], ‘.htm’) || ( SCRIPT_URI == CosSiteHome) ){
在本行下增加:
substr_count($_SERVER[’REQUEST_URI’], ‘comment’))
$is_buffer =
前面加上“//”注释掉本行
分别的原理及目的:
这个是在检测我们要静态化哪些文件,我为它增加了page、tag和category。当然,这个也和目录的模式有关,比如我的博客中,php的标签地址就是:http://www.guoanwei.org/tag/php。所以我就找到以ta-g开头的目录并静态化它们
防止评论内容被静态化
作者写了个函数,将singlepost(也就是单页页面)、首页都加上了一个安全标签(&!–cos-html-cache-safe-tag–&)。有这个标签的话将不被静态化。而我们需要它们静态化,所以就把这行注释掉好了
本人仅仅是尝试性修改,不保证其完全正确性。有兴趣的话大家可以试试看,呵呵。也欢迎一起交流。
Cos-html-cache插件能够将WordPress实现部分静态,包括最主要的首页和文章页面,目录页目前还没有静态化。
先通过修改永久链接格式将WordPress的链接结构修改为“伪HTML”格式,然后启用这个插件可以将文章页生成完全的静态HTML文章。这个插件还不能支持全静态化,但是文章页和首页的访问是最大的,这种静态化也可以极大的提高系统的效率,减少对数据库的访问量。
为了能够实现WordPress的标签和分类的静态化,需要修改一下这个插件的代码,修改方法是:
找到下面这一行:
if( substr_count($_SERVER[‘REQUEST_URI’], ‘.htm’) || ( SCRIPT_URI == CosSiteHome) ){
将其修改为:
if( substr_count($_SERVER[‘REQUEST_URI’], ‘page’) || substr_count($_SERVER[‘REQUEST_URI’], ‘tag’) || substr_count($_SERVER[‘REQUEST_URI’], ‘category’) || substr_count($_SERVER[‘REQUEST_URI’], ‘.htm’) || ( SCRIPT_URI == CosSiteHome) ){
在下面这一行:
substr_count($_SERVER[‘REQUEST_URI’], ‘../’))
$is_buffer =
的后面增加一行:
substr_count($_SERVER[‘REQUEST_URI’], ‘comment’))
$is_buffer =
找到下面这一行:
add_action(‘get_footer’, ‘CosSafeTag’);
用//将其注释。
总的来说,这个插件的工作效率比WP-Cache要高,对于系统的性能有很大的提高。
个博客站,包括企业站、图片站等等,里面的内容都可以归为两大类:文章、分类;文章又可以分为:普通文章、独立页面。在wordpress后台已经将文章和内容区分开了,实际上文章和页面只是两种文章形式,它们在数据库中存储在同一个表中,存储方式也是一样,只有一个属性不同:post,page。所以page的添加方式,显示方式跟posts一样,他们的模板代码页基本相似,只不过我们按照需要可以改的不一样。
在前面的教程中已经编辑过page.php文件了,里面的头部代码、侧边栏代码、底部代码已经添加好了,正文内容,你可以按照添加index.php的方法也可以按照添加single.php的方法。
添加过程就省略了。重复内容无意义。
但是还是有些内容要说。
我们在添加页面的时候,右边有个选项页面属性:
可以看到有个页面模板选项,一般只有默认模板,也就是我们的page.php,如果你想添加另外的模板,比如你将page.php复制一份,然后重命名为page-tep.php,
编辑page-tep.php在最最最前面加上
再进入后台编辑页面,就能看到有一个留言板选项了。
我们下载的主题文件包中提供了两个模板:无边栏页面full_width.php,联系页contact.php。
里面内容添加方法也一样。
最后提供露兜博主提供的修改完成的主题下载:
节目预报:
至此,wordpress主题制作基础教程结束了,欢迎继续关注wordpress主题制作教程之功能集成,再下节教程中,我们将讲述如何不适用插件给主题添加一些常用功能。
前面我们制作了文章单页模板,我们可以发现单页模板的代码跟index.php差不多,不过今天我们让它们的差别大一点,我们给文章模板加入评论表单,让访客可以发表评论。
首先在主题文件夹下面新建一个文件comments.php,然后打开single.php文件,将里面的评论代码剪切出来,粘贴到comments.php文件中,要剪切的代码如下:
&!– Comment’s List –&
&h3&Comments&/h3&
&div class=“hr dotted clearfix”&&&/div&
&ol class=“commentlist”&
&li class=“comment”&
&div class=“gravatar”& &img alt=“” src=’images/gravatar.png’ height=’48′ width=’48′ /& &a class=“comment-reply-link” href=&&Reply&/a& &/div&
&div class=“comment_content”&
&div class=“clearfix”& &cite class=“author_name”&&a href=“”&Joe Bloggs&/a&&/cite&
&div class=“comment-meta commentmetadata”&January 6, 2010 at 6:26 am&/div&
&div class=“comment_text”&
&p&Donec leo. Aliquam risus elit, luctus vel, interdum vitae, malesuada eget, elit. Nulla vitae ipsum. Donec ligula ante, bibendum sit amet, elementum quis, viverra eu, ante. Fusce tincidunt. Mauris pellentesque, arcu eget feugiat accumsan, ipsum mi molestie orci, ut pulvinar sapien lorem nec dui.&/p&
&div class=“hr clearfix”&&&/div&
&!– Comment Form –&
&form id=“comment_form” action=“” method=“post”&
&h3&Add a comment&/h3&
&div class=“hr dotted clearfix”&&&/div&
&li class=“clearfix”&
&label for=“name”&Your Name&/label&
&input id=“name” name=“name” type=“text” /&
&li class=“clearfix”&
&label for=“email”&Your Email&/label&
&input id=“email” name=“email” type=“text” /&
&li class=“clearfix”&
&label for=“email”&Your Website&/label&
&input id=“website” name=“website” type=“text” /&
&li class=“clearfix”&
&label for=“message”&Comment&/label&
&textarea id=“message” name=“message” rows= cols=&&/textarea&
&li class=“clearfix”&
&!– Add Comment Button –&
&a type=“submit” class=“button medium black right”&Add comment&/a& &/li&
然后在single.php文件原来的位置添加代码:
&?php comments_template(); ?&
comments_template()函数默认的就是加载主题文件夹下面的comments.php文件,这个函数也是可以带参数的,以便让你可以加载别的文件,比如某些页面你需要加载一个不一样的评论表单,你就需要使用comments_template()带上参数,这里不细说。
为了防止某些恶意用户直接打开评论文件,我们在comments.php的头部添加代码:
if (isset($_SERVER[‘SCRIPT_FILENAME’]) && ‘comments.php’ == basename($_SERVER[‘SCRIPT_FILENAME’]))
die (‘Please do not load this page directly. Thanks!’);
修改评论列表
wordpress有自动输出评论列表的函数wp_list_comments(),所以我们将原来的评论列表代码删除,换上这个函数,但是我们还需要加一些判断功能,比如评论需要密码才能查看、评论已经关闭、还没有评论这几个情况都要有不同的输出,所以将原来的评论代码:
&li class=“comment”&
&div class=“gravatar”& &img alt=“” src=’images/gravatar.png’ height=’48′ width=’48′ /& &a class=“comment-reply-link” href=&&Reply&/a& &/div&
&div class=“comment_content”&
&div class=“clearfix”& &cite class=“author_name”&&a href=“”&Joe Bloggs&/a&&/cite&
&div class=“comment-meta commentmetadata”&January 6, 2010 at 6:26 am&/div&
&div class=“comment_text”&
&p&Donec leo. Aliquam risus elit, luctus vel, interdum vitae, malesuada eget, elit. Nulla vitae ipsum. Donec ligul
a ante, bibendum sit amet, elementum quis, viverra eu, ante. Fusce tincidunt. Mauris pellentesque, arcu eget feugiat accumsan, ipsum mi molestie orci, ut pulvinar sapien lorem nec dui.&/p&
if (!emptyempty($post-&post_password) && $_COOKIE[‘wp-postpass_’ . COOKIEHASH] != $post-&post_password) {
&li class=“decmt-box”&
&p&&a href=“#addcomment”&请输入密码再查看评论内容.&/a&&/p&
} else if ( !comments_open() ) {
&li class=“decmt-box”&
&p&&a href=“#addcomment”&评论功能已经关闭!&/a&&/p&
} else if ( !have_comments() ) {
&li class=“decmt-box”&
&p&&a href=“#addcomment”&还没有任何评论,你来说两句吧&/a&&/p&
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; ?&
&li class=“comment” id=“li-comment-&?php comment_ID(); ?&”&
&div class=“gravatar”& &?php if (function_exists(‘get_avatar’) && get_option(‘show_avatars’)) { echo get_avatar($comment, 48); } ?&
&?php comment_reply_link(array_merge( $args, array(‘reply_text’ =& ‘回复’,’depth’ =& $depth, ‘max_depth’ =& $args[‘max_depth’]))) ?& &/div&
&div class=“comment_content” id=“comment-&?php comment_ID(); ?&”&
&div class=“clearfix”&
&?php printf(__(‘&cite class=“author_name”&%s&/cite&’), get_comment_author_link()); ?&
&div class=“comment-meta commentmetadata”&发表于:&?php echo get_comment_time(‘Y-m-d H:i’); ?&&/div&
&&&&?php edit_comment_link(‘修改’); ?&
&div class=“comment_text”&
&?php if ($comment-&comment_approved == ) : ?&
&em&你的评论正在审核,稍后会显示出来!&/em&&br /&
&?php endif; ?&
&?php comment_text(); ?&
&?php } ?&
上面的自定义函数中用到的几个函数的说明如下:
get_avatar($id_or_email,$size,$default, $alt);
&?php comment_reply_link();
get_comment_author_link();
get_comment_time();
edit_comment_link();
comment_text();
添加了上面的代码评论已经能正确显示了,接下来添加提交评论的表单。
将原来comments.php中的评论表单代码删除:
&!– Comment Form –&
&form id=“comment_form” action=“” method=“post”&
&h3&Add a comment&/h3&
&div class=“hr dotted clearfix”&&&/div&
&li class=“clearfix”&
&label for=“name”&Your Name&/label&
&input id=“name” name=“name” type=“text” /&
&li class=“clearfix”&
&label for=“email”&Your Email&/label&
&input id=“email” name=“email” type=“text” /&
&li class=“clearfix”&
&label for=“email”&Your Website&/label&
&input id=“website” name=“website” type=“text” /&
&li class=“clearfix”&
&label for=“message”&Comment&/label&
&textarea id=“message” name=“message” rows= cols=&&/textarea&
&li class=“clearfix”&
&!– Add Comment Button –&
&a type=“submit” class=“button medium black right”&Add comment&/a& &/li&
实际上你不需要再手动输入每个表单项了,新版的wordprss提供了一个非常方便的函数:comment_form(),添加代码如下:
&?php if ( comments_open() ) : ?&
&?php if ( get_option(‘comment_registration’) && !$user_ID ) : ?&
&p&&?php printf(__(‘你需要先 &a href=“%s”&登录&/a& 才能发表评论.’), get_option(‘siteurl’).“/wp-login.php?redirect_to=”.urlencode(get_permalink()));?&&/p&
&?php else : ?&
&?php $defaults = array(
‘comment_notes_before’ =& ”,
‘label_submit’
=& __( ‘提交评论’ ),
‘comment_notes_after’ =&”
comment_form($defaults);
&p&&?php _e(‘对不起评论已经关闭.’); ?&&/p&
&?php endif; ?&
可以看到上面的代码中也添加了判断,看是否允许评论,是否需要登录才能评论。
你完全可以通过comment_form()函数的各个参数再配合css输出一个个性化的表单,这在以后的教程中讲。
前一篇教程中我们已经只做好了index.php,这个文件可以当首页使用,也可以当分类、标签等归档页使用,这篇教程我们来制作一下文章的具体页面-文章单页模板,如果我们没有但页模板single.php,那么就会使用index.php文件来代替,不过对于文章单页,我们还需要添加一些其他的信息,比如版权申明、相关文章、评论等等,所以我们应该另外制作一个模板,在我们下载的主题文件夹下面已经有了single.php文件。你可以试着删除这个文件,然后再去看看用index.php显示文章的效果。
用编辑器打开主题文件夹下面的single.php文件,在前面的教程中我们已经将这个文件中的头部、底部、侧边栏代码替换成了加载对应模板的代码。对于文章单页,我们的文章框架代码页需要放在一个循环中,只不过到了单页面,只循环一次。所以你完全可以将index.php里面的代码全部复制过来,再添加,修改。
一、文章标题
找到文章标题:
&h3 class=“title”&&a href=“single.html”&Loreum ipsium massa cras phasellus&/a&&/h3&
还记得我们上篇教程讲的获取文章链接、标题的代码吗?
&h3 class=“title”&&a href=“&?php the_permalink(); ?&”&&?php the_title(); ?&&/a&&/h3&
二、文章标签:
&a href=“#”&News&/a&, &a href=“#”&Products&/a&
&?php the_tags(‘标签:’, ‘, ‘, ”); ?&
将日期改为:
&?php the_time(‘Y年n月j日’) ?&
四、评论数
&a href=“#”&7 Comments&/a&
&?php comments_popup_link(‘0 条评论’, ‘1 条评论’, ‘% 条评论’, ”, ‘评论已关闭’); ?&
五、文章内容。
先将文章的图片删了,删除下面的代码:
&img class=“thumb” src=“&?php bloginfo(‘template_url’); ?&/images/610×150.gif” alt=“”/&
然后将所有文章内容,即: 和 之间的代码全部删除,替换成:
&?php the_content(); ?&
六、评论和返回首页
&p class=“clearfix”& &a href=“blog.html” class=“button float” &&& Back to Blog&/a& &a href=“#commentform” class=“button float right” &Discuss this post&/a& &/p&
&p class=“clearfix”& &a href=“&?php echo get_option(‘home’); ?&” class=“button float” &&& 返回首页&/a& &a href=“#commentform” class=“button float right” &发表评论&/a& &/p&
好了,前面说过文章单页的内容页需要放在一个循环语句中(事实上是我们需要在输出文章的前面执行the_post()函数,这个函数会生成文章变量$post)。
在的后面添加代码,效果:
&!– Column 1 /Content –&
&?php if (have_posts()) : the_post(); update_post_caches($posts); ?&
然后在代码
&?php get_sidebar(); ?&
的前面,注意咯。“的前面”添加代码,完成效果:
&?php else : ?&
&div class=“errorbox”&
没有文章!
&?php endif; ?&
&?php get_sidebar(); ?&
这里的操作跟首页差不多,不过这里只需要输出一篇文章,所以while添加与否没有多大关系,需要提醒的是,一定要记住添加了if,就得有endif,添加了while,就得有endwhile。
可能其他语法不会这样用,其实这里你也可以改成用{}的。比如:
&?php if( have_posts() ){ the_post();?&
&!–文章代码–&
&?php } ?&
OK,文章单页制作方法就完成了。
我们前面已经将index.php文件里面的公用代码提取出来制作成了header.php\footer.php\sidebar.php,现在我们就来整理一下index.php的代码,index.php文件算是一个最普遍使用的模板页面了,如果你的主题没有home.php、且后台设置首页显示最新文章,那么index.php文件就是首页模板了,如果你的主题没有文章也模板(single.php)、没有单页面模板(page.php)、没有分类页模板(category.php)、没有标签页(index.php)……没有404页面等的,都将会使用index.php文件代替。
那么我们今天要整理的index.php文件到底要以什么形式来显示呢?一般来说都是文章列表,这样这个文件做首页能正确显示、还能做归档页、搜索结果页等。。
不过我们首页如果需要显示20篇文章?我们是不是需要写20篇文章的代码呢?其实这20篇文章代码都是同样的形式,所以我们只需要写一篇文章的代码,然后将这个代码循环输出就好了。如果你之前学过任何一门编程语言,那么while\for循环应该不陌生,条件语句if也应该不陌生。
下面用编辑器打开index.php文件,可以看到里面有3篇文章的代码,我们将其中两篇代码删除,只留下一篇、并且将摘要文字删除:
&?php get_header(); ?&
&!– Column 1 /Content –&
&div class=“grid_8″&
&!– Blog Post –&
&div class=“post”&
&!– Post Title –&
&h3 class=“title”&&a href=“single.html”&Loreum ipsium massa cras phasellus&/a&&/h3&
&!– Post Data –&
&p class=“sub”&&a href=“#”&News&/a&, &a href=“#”&Products&/a& & 31st Sep, 09 & &a href=“#”&1 Comment&/a&&/p&
&div class=“hr dotted clearfix”&&&/div&
&!– Post Image –&
&img class=“thumb” alt=“” src=“&?php bloginfo(‘template_url’); ?&/images/610×150.gif” /&
&!– Post Content –&
&!– Read More Button –&
&p class=“clearfix”&&a href=“single.html” class=“button right”& Read More…&/a&&/p&
&div class=“hr clearfix”&&&/div&
&!– Blog Navigation –&
&p class=“clearfix”& &a href=“#” class=“button float”&&& Previous Posts&/a& &a href=“#” class=“button float right”&Newer Posts &&&/a& &/p&
&?php get_sidebar(); ?&
&?php get_footer(); ?&
我们可以看到,实际上文章骨架,也就是每篇文章都需要的那个代码框架,当然这里说的仅仅是只的这个主题,如果主题的样式不同那html代码的结构也不一样,代码如下:
&div class=“post”&
&!– Post Title –&
&h3 class=“title”&&a href=“single.html”&文章标题&/a&&/h3&
&!– Post Data –&
&p class=“sub”&&a href=“#”&标签1&/a&, &a href=“#”&标签12&/a& & 发布时间 & &a href=“#”&评论数&/a&&/p&
&div class=“hr dotted clearfix”&&&/div&
&!– Post Image 文章的缩略图 –&
&img class=“thumb” alt=“” src=“&?php bloginfo(‘template_url’); ?&/images/610×150.gif” /&
&!– Post Content –&
&!– Read More Button –&
&p class=“clearfix”&&a href=“single.html” class=“button right”& 阅读全文按钮&/a&&/p&
&div class=“hr clearfix”&&&/div&
接下来我们将里面的静态内容改成动态的。
一、文章标题
将文章标题代码:
&h3 class=“title”&&a href=“single.html”&Loreum ipsium massa cras phasellus&/a&&/h3&
&h3 class=“title”&&a href=“&?php the_permalink(); ?&” rel=“bookmark”&&?php the_title(); ?&&/a&&/h3&
里面的代码php函数:the_permalink();是输出当前文章的链接地址,注意是直接输出;the_title();函数直接输出当前文章的标题。
二、文章标签
将index.php里面的标签代码:
&a href=“#”&News&/a&, &a href=“#”&Products&/a&
&?php the_tags(‘标签:’, ‘, ‘, ”); ?&
the_tags函数直接输出文章标签。
找到日期文字,31st Sep, 09直接替换成:
&?php the_time(‘Y年n月j日’) ?&
the_time函数直接输出文章日期,至于输出格式Y年n月j日可以改你也可以改成Y-n-j这些参数非常多,请自己到官网查询。
四、评论数
在文章归档页显示文章的评论数和点击数似乎很是流行,将里面的评论代码
&a href=“#”&1 Comment&/a&
&?php comments_popup_link(‘0 条评论’, ‘1 条评论’, ‘% 条评论’, ”, ‘评论已关闭’); ?&
comments_popup_link()函数里面的三个参数分别代表输出无评论、一条评论、N条评论,里面那个%相当于占位符了。这个函数输出的代码带有链接,会链接到文章页,并定位到评论位置。
五、文章内容
在文章内容的位置添加代码
&?php the_content(‘阅读全文…’); ?&
即可,事实上我们要输出的是摘要,而the_content是输出文章内容的,但是在首页和归档页,如果你在文章中添加了more标签,则会输出more标签之前的内容,并且在后面加上一个“阅读全文”的链接。但是很多人会想到使用另一个输出摘要的函数the_excerpt();我不建议你这样做,这个函数会输出文章的摘要(也就是在后台添加文章的时候有一个专门用来添加摘要的地方),如果没有摘要的话,就会自动截取前50个字符,不过这是对于英文而言,对于中文的多字节语言,这个函数是截取不了的,所以他会全文输出,相比而言,添加More标签更麻烦还是填写摘要更麻烦呢?不过如果你打算每篇文章手动指定一个摘要的话,建议你使用the_excerpt函数。
六、文章循环
前面的代码我们已经将一篇文章的框架写好了,现在要做的就是将这个文章框架代码放在一个循环语句中输出。
在文章框架的前面,也就是有注释&!-- Blog Post --&的地方,添加代码,效果:
&!– Blog Post –&
&?php if (have_posts()) : while (have_posts()) : the_post(); ?&
再在文章框架后面,添加结束循环的代码,找到:
&div class=“hr clearfix”&&&/div&
&div class=“hr clearfix”&&&/div&
&?php endwhile; ?&
&?php get_sidebar(); ?&
&?php else : ?&
&h3 class=“title”&&a href=“#” rel=“bookmark”&未找到&/a&&/h3&
&p&没有找到任何文章!&/p&
&?php endif; ?&
?php get_sidebar(); ?&
OK,到此为止我们的循环代码已经完成,分析一下我们刚才添加的代码,大致是这样子的:
&?php if (have_posts()) : while (have_posts()) : the_post(); ?&
文章html骨架
&?php endwhile; ?&
&?php else : ?&
输出找不到文章提示
&?php endif; ?&
have_posts()函数是判断当前是否有文章:当前页面要输出的所有文章存放在一个全局数组$posts中,have_post()函数就是检查这个数组的一个计数器,如果当前还有文章,那么就返回true,如果没有就返回false;
the_post()函数用来将have_posts计数器前移,并且将当前文章填进变量$post中,而前面的函数the_title(),the_content()这些函数只是用用来输出$post变量中的的内容,你完全可以用
&?php echo $post-&?&
来代替the_title()函数,你也可以输出$post变量中的其它内容,比如文章ID。
七、文章分页
前面的代码一次只能输出部分文章,如果整个博客有100篇文章,不可能将100篇文章全部列出来,这时候就需要分页显示了。
找到我们的分页代码:
&p class=“clearfix”& &a href=“#” class=“button float”&&& Previous Posts&/a& &a href=“#” class=“button float right”&Newer Posts &&&/a& &/p&
&p class=“clearfix”&&?php previous_posts_link(‘&& 查看新文章’, 0); ?& &span class=“float right”&&?php next_posts_link(‘查看旧文章 &&’, 0); ?&&/span&&/p&
下面提供露兜博主经过经过本次修改后的主题文件
前面两篇教程讲到了将头部和底部公用的代码提取到单独的一个文件中,同样的道理,对于博客主题来说,侧边栏也基本是公用的(也许有些页面不一样),一般来说我们也将侧边栏公用的代码提取出来放到一个单独的文件中,当然侧边栏的功能不仅仅是这样,有了侧边栏文件,通过代码可以从后台往侧边栏添加小工具。
在主题文件夹content\themes\Aurelius中新建一个sidebar.php文件,然后用编辑器打开index.php文件将里面的侧边栏代码剪切出来,粘贴到sidebar.php中,侧边栏代码如下:
&!– Column 2 / Sidebar –&
&div class=“grid_4″&
&h4&Catagories&/h4&
&ul class=“sidebar”&
&li&&a href=“”&So who are we?&/a&&/li&
&li&&a href=“”&Philosophy&/a&&/li&
&li&&a href=“”&History&/a&&/li&
&li&&a href=“”&Jobs&/a&&/li&
&li&&a href=“”&Staff&/a&&/li&
&li&&a href=“”&Clients&/a&&/li&
&h4&Archives&/h4&
&ul class=“sidebar”&
&li&&a href=“”&January 2010&/a&&/li&
&li&&a href=“”&December 2009&/a&&/li&
&li&&a href=“”&Novemeber 2009&/a&&/li&
&li&&a href=“”&October 2009&/a&&/li&
&li&&a href=“”&September 2009&/a&&/li&
&li&&a href=“”&August 2009&/a&&/li&
&div class=“hr grid_12 clearfix”&&&/div&
剪切之后,在index.php原来的位置加上代码:
&?php get_sidebar(); ?&
可以看到这个函数跟获取头部、底部函数灰常相似。get_sidebar()函数会加载sidebar.php文件,不过get_sidebar()函数是可以加参数的。
&?php get_sidebar(1); ?&
这个代码加载sidebar-1.php,有的人希望网站上的首页、内页、分类页等各个页面的侧边栏不一样,这样就需要有多个侧边栏,这时候就得给这个函数加参数了。
为了适应WordPress程序,我们还要对sidebar.php做一些微调,下载新的样式表style.css,替换Aurelius目录下的style.css,下面提供露兜博客上的新的style.css文件下载链接:
编辑sidebar.php文件,将里面的代码删除,因为那些都是静态的,我们需要从后台设置小工具,所以删除了,改成:
&!– Column 2 / Sidebar –&
&div class=“grid_4″&
&?php if ( !function_exists(‘dynamic_sidebar’)
|| !dynamic_sidebar(‘First_sidebar’) ) : ?&
&h4&分类目录&/h4&
&?php wp_list_categories(‘depth=1&title_li=&orderby=id&show_count=0&hide_empty=1&child_of=0′); ?&
&?php endif; ?&
&?php if ( !function_exists(‘dynamic_sidebar’)
|| !dynamic_sidebar(‘Second_sidebar’) ) : ?&
&h4&最新文章&/h4&
$posts = get_posts(‘numberposts=6&orderby=post_date’);
foreach($posts as $post) {
setup_postdata($post);
echo ‘&li&&a href=“‘ . get_permalink() . ‘”&’ . get_the_title() . ‘&/a&&/li&';
$post = $posts[0];
&?php endif; ?&
&?php if ( !function_exists(‘dynamic_sidebar’)
|| !dynamic_sidebar(‘Third_sidebar’) ) : ?&
&h4&标签云&/h4&
&p&&?php wp_tag_cloud(‘smallest=8&largest=22′); ?&&/p&
&?php endif; ?&
&?php if ( !function_exists(‘dynamic_sidebar’)
|| !dynamic_sidebar(‘Fourth_sidebar’) ) : ?&
&h4&文章存档&/h4&
&?php wp_get_archives(‘limit=10′); ?&
&?php endif; ?&
&div class=“hr grid_12 clearfix”&&&/div&
仅仅有代码是不够的,还需要函数支持,现在在主题文件夹下面新建一个文件functions.php 用来放函数代码,在里面添加代码:
if ( function_exists(‘register_sidebar’) ) {
register_sidebar(array(
‘name’=&’首页侧边栏’,
‘before_widget’ =& ‘&li id=“%1$s” class=“sidebar_li %2$s”&’,
‘after_widget’ =& ‘&/li&’,
‘before_title’ =& ‘&h3&’,
‘after_title’ =& ‘&/h3&’,
通过添加函数注册一个侧边栏,添加了这个函数,你的主题就支持侧边栏功能了,在后台小工具页面就能看到有侧边栏选项。
下面提供露兜博主的functions.php文件:
OK,现在你可以在后台往侧边栏里面拖放小工具了,然后去前台看看效果。。
下面提供经过本次修改的主题文件包
下载该文件
前面我们将所有模板文件公用的头部代码放到一个文件中(header.php),同样的道理,我们的底部信息,像版权申明等也基本上是所有页面公用的,所以我们也将这部分代码提取出来放在一个单独的php文件中。
在我们的主题文件夹content\themes\Aurelius新建一个footer.php文件。
然后用编辑器打开index.php文件将里面的代码:
&!– Footer –&
&p class=“grid_12 footer clearfix”& &span class=“float”&&strong&Design By&/strong& QwibbleDesigns&&&&&&&strong&Code By&/strong& &a href=“http://www.ludou.org/”&Ludou&/a&&/span& &a class=“float right” href=“#”&top&/a& &/p&
&!–end wrapper–&
剪切了,然后粘贴到footer.php中,同样剪切了代码之后,我们需要加载这个footer.php,在index.php文件的最后面添加代码:
&?php get_footer(); ?&
与get_header()函数对应,这个函数就是加载主题文件夹中的footer.php文件。
接下来将archive.php、contact.php、full_width.php、page.php和single.php中对应的代码也改了。
接下来修改footer.php文件,将里面的代码替换成:
&!– Footer –&
&p class=“grid_12 footer clearfix”&
&span class=“float”&版权所有 © 2010 &?php bloginfo(‘name’); ?&
&&|&&Powered By &a rel=“external” title=“WordPress主页” class=“link” href=“http://wordpress.org/”&WordPress&/a&
&&|&&Design By QwibbleDesigns&&|&&
Code By &a href=“http://www.ludou.org/”&Ludou&/a&
&a class=“float right” href=“#”&top&/a& &/p&
&!–end wrapper–&
&?php wp_footer(); ?&
这里用到了我们上次学到的bloginfo(‘name’)函数来输出你的博客标题,wp_footer()和wp_head()差不多,都是用于提高你的主题兼容性,毕竟有很多插件要在页脚输出一些东西才能正常工作。现在你的页脚应该是有模有样了吧,下面是露兜博主更改后的效果:
好了,footer.php文件里面的代码可以自由发挥。。自由修改。。

我要回帖

更多关于 wordpress主题 的文章

 

随机推荐