WordPress主题制作课程#9:制作索引页文件index.php

WordPress教程 1424

博客吧前面的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> &bull; 31st Sep, 09 &bull; <a href="#">1 Comment</a></p>
            <div class="hr dotted clearfix">&nbsp;</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">&nbsp;</div>
 
        <!-- Blog Navigation -->
        <p class="clearfix"> <a href="#" class="button float">&lt;&lt; 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> &bull; 发布时间 &bull; <a href="#">评论数</a></p>
    <div class="hr dotted clearfix">&nbsp;</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">&nbsp;</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('编辑', ' &bull; ', ''); ?>
添加文章内容

可能有些人喜欢在首页输出全文,而有些人喜欢在首页输出文章摘要,这里提供两种方案,任君选择。查找:

1
<!-- Post Content -->

将其改成:

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
<!-- Blog Post -->

改成:

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

再查找:

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

改成:

1
2
<div class="hr clearfix">&nbsp;</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">&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>
文章缩略图

对于大部分人来说,不太需要文章缩略图的功能,而且有很多插件可以实现这个功能。这里我们将首页的文章缩略图代码删除:

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

教程摘自露兜博客

精品推荐: