wordpress主题制作教程(九):制作index.php文件

我们前面已经将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篇文章的代码,我们将其中两篇代码删除,只留下一篇、并且将摘要文字删除:

  1. <?php get_header(); ?>   
  2.     <!-- Column 1 /Content -->   
  3.     <div class="grid_8">   
  4.         <!-- Blog Post -->   
  5.         <div class="post">   
  6.             <!-- Post Title -->   
  7.             <h3 class="title"><a href="single.html">Loreum ipsium massa cras phasellus</a></h3>   
  8.             <!-- Post Data -->   
  9.             <p class="sub"><a href="#">News</a>, <a href="#">Products</a> &bull; 31st Sep, 09 &bull; <a href="#">1 Comment</a></p>   
  10.             <div class="hr dotted clearfix">&nbsp;</div>   
  11.             <!-- Post Image -->   
  12.             <img class="thumb" alt="" src="<?php bloginfo('template_url'); ?>/images/610x150.gif" />   
  13.             <!-- Post Content -->   
  14.                
  15.             <!-- Read More Button -->   
  16.             <p class="clearfix"><a href="single.html" class="button right"> Read More...</a></p>   
  17.         </div>   
  18.         <div class="hr clearfix">&nbsp;</div>   
  19.            
  20.         <!-- Blog Navigation -->   
  21.         <p class="clearfix"> <a href="#" class="button float">&lt;&lt; Previous Posts</a> <a href="#" class="button float right">Newer Posts >></a> </p>   
  22.     </div>   
  23.     <?php get_sidebar(); ?>   
  24. <?php get_footer(); ?>  

我们可以看到,实际上文章骨架,也就是每篇文章都需要的那个代码框架,当然这里说的仅仅是只的这个主题,如果主题的样式不同那html代码的结构也不一样,代码如下:

  1. <div class="post">   
  2.     <!-- Post Title -->   
  3.     <h3 class="title"><a href="single.html">文章标题</a></h3>   
  4.     <!-- Post Data -->   
  5.     <p class="sub"><a href="#">标签1</a>, <a href="#">标签12</a> &bull; 发布时间 &bull; <a href="#">评论数</a></p>   
  6.     <div class="hr dotted clearfix">&nbsp;</div>   
  7.     <!-- Post Image 文章的缩略图 -->   
  8.     <img class="thumb" alt="" src="<?php bloginfo('template_url'); ?>/images/610x150.gif" />   
  9.     <!-- Post Content -->   
  10.     文章内容   
  11.     <!-- Read More Button -->   
  12.     <p class="clearfix"><a href="single.html" class="button right"> 阅读全文按钮</a></p>   
  13. </div>   
  14. <div class="hr clearfix">&nbsp;</div>  

接下来我们将里面的静态内容改成动态的。
一、文章标题
将文章标题代码:

  1. <h3 class="title"><a href="single.html">Loreum ipsium massa cras phasellus</a></h3>  

替换成

  1. <h3 class="title"><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></h3>  

里面的代码php函数:the_permalink();是输出当前文章的链接地址,注意是直接输出;the_title();函数直接输出当前文章的标题。
二、文章标签
将index.php里面的标签代码:

  1. <a href="#">News</a>, <a href="#">Products</a>  

替换成:

  1. <?php the_tags('标签:', ', ', ''); ?>  

the_tags函数直接输出文章标签。
三、日期
找到日期文字,31st Sep, 09直接替换成:

  1. <?php the_time('Y年n月j日') ?>  

the_time函数直接输出文章日期,至于输出格式Y年n月j日可以改你也可以改成Y-n-j这些参数非常多,请自己到官网查询。
四、评论数
在文章归档页显示文章的评论数和点击数似乎很是流行,将里面的评论代码

  1. <a href="#">1 Comment</a>  

替换成

  1. <?php comments_popup_link('0 条评论', '1 条评论', '% 条评论', '', '评论已关闭'); ?>  

comments_popup_link()函数里面的三个参数分别代表输出无评论、一条评论、N条评论,里面那个%相当于占位符了。这个函数输出的代码带有链接,会链接到文章页,并定位到评论位置。
五、文章内容
在文章内容的位置添加代码

  1. <?php the_content('阅读全文...'); ?>  

即可,事实上我们要输出的是摘要,而the_content是输出文章内容的,但是在首页和归档页,如果你在文章中添加了more标签,则会输出more标签之前的内容,并且在后面加上一个“阅读全文”的链接。但是很多人会想到使用另一个输出摘要的函数the_excerpt();我不建议你这样做,这个函数会输出文章的摘要(也就是在后台添加文章的时候有一个专门用来添加摘要的地方),如果没有摘要的话,就会自动截取前50个字符,不过这是对于英文而言,对于中文的多字节语言,这个函数是截取不了的,所以他会全文输出,相比而言,添加More标签更麻烦还是填写摘要更麻烦呢?不过如果你打算每篇文章手动指定一个摘要的话,建议你使用the_excerpt函数。

六、文章循环

前面的代码我们已经将一篇文章的框架写好了,现在要做的就是将这个文章框架代码放在一个循环语句中输出。

在文章框架的前面,也就是有注释<!-- Blog Post -->的地方,添加代码,效果:

  1. <!-- Blog Post -->   
  2.         <?php if (have_posts()) : while (have_posts()) : the_post(); ?>  

再在文章框架后面,添加结束循环的代码,找到:

  1. <div class="hr clearfix">&nbsp;</div>  

修改为

  1. <div class="hr clearfix">&nbsp;</div>   
  2.         <?php endwhile; ?>  

再找到:

  1. </div>   
  2.     <?php get_sidebar(); ?>  

修改为:

  1. <?php else : ?>   
  2.        <h3 class="title"><a href="#" rel="bookmark">未找到</a></h3>   
  3.        <p>没有找到任何文章!</p>   
  4.        <?php endif; ?>   
  5.    </div>   
  6. ?php get_sidebar(); ?>  

OK,到此为止我们的循环代码已经完成,分析一下我们刚才添加的代码,大致是这样子的:

  1. <?php if (have_posts()) : while (have_posts()) : the_post(); ?>   
  2.     文章html骨架   
  3. <?php endwhile; ?>   
  4. <?php else : ?>   
  5.     输出找不到文章提示   
  6. <?php endif; ?>  

have_posts()函数是判断当前是否有文章:当前页面要输出的所有文章存放在一个全局数组$posts中,have_post()函数就是检查这个数组的一个计数器,如果当前还有文章,那么就返回true,如果没有就返回false;
the_post()函数用来将have_posts计数器前移,并且将当前文章填进变量$post中,而前面的函数the_title(),the_content()这些函数只是用用来输出$post变量中的的内容,你完全可以用

  1. <?php echo $post->title;?>  

来代替the_title()函数,你也可以输出$post变量中的其它内容,比如文章ID。
七、文章分页
前面的代码一次只能输出部分文章,如果整个博客有100篇文章,不可能将100篇文章全部列出来,这时候就需要分页显示了。
找到我们的分页代码:

  1. <p class="clearfix"> <a href="#" class="button float">&lt;&lt; Previous Posts</a> <a href="#" class="button float right">Newer Posts >></a> </p>  

替换成

  1. <p class="clearfix"><?php previous_posts_link('&lt;&lt; 查看新文章', 0); ?> <span class="float right"><?php next_posts_link('查看旧文章 &gt;&gt;', 0); ?></span></p>  

下面提供露兜博主经过经过本次修改后的主题文件
下载改文件

已有6条评论

发表评论