博客吧前面的 wordpress 主题制作课程已经制作完成 wordpress 博客主题所需要的公众页面 sidebar.php、footer.php 和 header.php 文件,现在就要开始制作索引页文件 index.php,对于刚刚学习制作主题的人,可以简单地把文件该文件理解成网站首页文件,虽然它并不仅仅是首页这么简单。
在博客首页中主要的是显示博客最新文章列表,即是把博客的文章一篇一篇地列出来,普通博客主题的主页中每篇文章的显示样式除了标题、摘要或时间等内容元素以外,显示格式都是一样的,因此制作 index.php 文件时只需要一篇文章的 html 代码即可,然后通过一个循环就可以将所有文章显示到首页上(循环就是重复做某件事情,这里的循环是重复地输出文章)。
现在开始制作 index.php 文件
初始情况下 index.php 中有三篇文章,打开 index.php 你可以看到文章的 3 个标记夹<!-- Blog Post -->
,我们现在其他将两篇文章的代码删除,留下一篇,并将文章摘要去除。修改后的代码是如下:
1 2
3
4
5
6
7
8
9
10
11 12
13
14 15
16
17
18
19
20
21 22
23
24
|
<?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/610x150.gif" />
<!-- Post Content -->
<!-- Read More Button -->
<p class="clearfix"><a href="single.html" class="button right"> Read More...</a></p>
</div>
<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>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
|
从上面的代码可以看出,一篇文章的 html 结构就是:
1 2
3
4
5
6
7
8
9
10
11 12
13
14
|
<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/610x150.gif" />
<!-- Post Content -->
文章内容
<!-- Read More Button -->
<p class="clearfix"><a href="single.html" class="button right"> 阅读全文按钮</a></p>
</div>
<div class="hr clearfix"> </div>
|
不同主题的文章 html 结构是不一样的,以上是我们这个主题的 HTML 结构,我们将以此为基础给 index.php 加上动态内容:
添加文章标题
找到:
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 函数注释:
- <?php the_permalink(); ?> 输出文章的 URL 链接
- <?php the_title(); ?> 输出文章的标题
添加文章标签
找到:
1
|
<a href="#">News</a>, <a href="#">Products</a>
|
改成
1
|
<?php the_tags('标签:', ',', ''); ?>
|
添加发表日期
找到:
31st Sep, 09
改成:
1
|
<?php the_time('Y 年 n 月 j 日') ?>
|
函数参考:the_time
显示评论数
现在我们来添加评论数代码,查找代码:
1
|
<a href="#">1 Comment</a>
|
改成:
1
|
<?php comments_popup_link('0 条评论', '1 条评论', '% 条评论', '', ' 评论已关闭 '); ?>
|
该函数会根据文章的评论数量显示不同的文字链接,0 条评论、1 条评论等等,当然能你可以根据自己的爱好定制文字内容。
函数参考:comments_popup_link
添加编辑按钮
如果文章作者已登录,我们将允许他在首页点击对应文章的编辑按钮,就可以直接修改文章,这个功能是可选的,你可以不添加。接上面的评论按钮,我们在其后面添加相应代码:
1
|
<?php comments_popup_link('0 条评论', '1 条评论', '% 条评论', '', ' 评论已关闭 '); ?> <?php edit_post_link(' 编辑 ', ' • ', ''); ?>
|
添加文章内容
可能有些人喜欢在首页输出全文,而有些人喜欢在首页输出文章摘要,这里提供两种方案,任君选择。查找:
将其改成:
1 2
|
<!-- Post Content -->
<?php the_excerpt(); ?>
|
只要在写文章的时候在 ” 摘要 ” 框内填写摘要,在首页显示的就是摘要,如果不填就输出全文!以下是方案二,用于输出全文,除非你在文章中使用了 <!– more –>,代码修改:
1 2
|
<!-- Post Content -->
<?php the_content('阅读全文...'); ?>
|
阅读全文
这里给添加阅读全文链接,如果在 6、添加文章内容中你选择了输出全文,本步骤可以忽略,查找代码:
1
|
<a href="single.html" class="button right"> Read More...</a>
|
改成:
1
|
<a href="<?php the_permalink(); ?>" class="button right"> 阅读全文 </a>
|
添加文章循环
到目前为止,你的首页还只能显示一篇文章,要想输出所有文章,需要我们之前提到的循环。查找代码:
改成:
1 2
|
<!-- Blog Post -->
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
|
再查找:
1
|
<div class="hr clearfix"> </div>
|
改成:
1 2
|
<div class="hr clearfix"> </div>
<?php endwhile; ?>
|
再次查找:
1 2
|
</div>
<?php get_sidebar(); ?>
|
改成:
1 2
3
4
5
6
|
<?php else : ?>
<h3 class="title"><a href="#" rel="bookmark"> 未找到 </a></h3>
<p> 没有找到任何文章!</p>
<?php endif; ?>
</div>
<?php get_sidebar(); ?>
|
现在查看博客的主页,可以发现显示了多篇文章,文章的显示数量可以在后台——设置——阅读中的每页最多显示里修改。
以上的循环可以简化为以下内容,这样看起来应该比较容易理解了,在 endwhile 之前不断地输出每篇文章,直至文章数量达到每页显示的最大文章数量;如果你的博客上一篇文章都没有,就会输入无文章提示。
1 2
3
4
5
6
|
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
文章 html 骨架
<?php endwhile; ?>
<?php else : ?>
输出找不到文章提示
<?php endif; ?>
|
添加分页
上面你已经看到,每页只能显示部分文章,要想看下一页,就得添加分页。现在查找代码:
1
|
<p class="clearfix"> <a href="#" class="button float"><< Previous Posts</a> <a href="#" class="button float right">Newer Posts >></a> </p>
|
改成:
1
|
<p class="clearfix"><?php previous_posts_link('<< 查看新文章', 0); ?> <span class="float right"><?php next_posts_link('查看旧文章 >>', 0); ?></span></p>
|
文章缩略图
对于大部分人来说,不太需要文章缩略图的功能,而且有很多插件可以实现这个功能。这里我们将首页的文章缩略图代码删除:
1 2
|
<!-- Post Image -->
<img class="thumb" alt="" src="<?php bloginfo('template_url'); ?>/images/610x150.gif" />
|
另外,还有个存档页面的模板 archive.php,跟 index.php 的制作过程完全一样,只不过需要在 functions.php 里添加一个函数,可以下载博客吧提供的完整文件查看,需要注意的是:functions.php 中的代码已经修改,要想让你的分类、标签等存档页能够正常显示,请下载最新的 functions.php 覆盖原来的。另外,标签页和分类页支持在该页面顶部显示介绍,前提是你在后台文章标签和分类处要填上了描述。
本次修改后的主题文件下载:aurelius_index.zip
教程摘自 露兜博客