Home >CMS Tutorial >WordPress >The whole process of WordPress theme creation (8): making index.php

The whole process of WordPress theme creation (8): making index.php

藏色散人
藏色散人forward
2023-02-21 10:00:551907browse

I introduced you to "The whole process of WordPress theme production (7): Making sidebar.php". This article continues to introduce to you how to make index.php. Let's take a look. ~

The whole process of WordPress theme creation (8): making index.php

We have already created sidebar.php, footer.php and header.php for all public pages of the blog. Starting today, we will create separate pages. Now what we want to create is the index page index.php. Here we can temporarily understand it as the home page, but in fact it is not as simple as the home page.

The main page is the article list, which lists the articles on your blog one by one. You may have noticed that the style of each article on the homepage is the same, but the text content such as title, time, author, and abstract are different, huh! We only need the HTML code of one article to make index.php. We don't need to manually write the HTML of so many articles. Moreover, this is not dynamic content. We only need one loop to display all articles on the home page. A loop is to do something repeatedly. The loop here is to repeatedly output articles. If you have learned any computer programming language before, it is not difficult to understand what a loop is, and the functions of a loop can be easily understood, such as while, for, foreach...

在Insert a sentence here, if you really want to know how to make a theme, please open the text editor, follow me step by step, modify step by step, refresh your blog every time you make a modification to see what changes, so Only then can you deepen your understanding. If you're too lazy to do it, I suggest you don't read the following content, as it won't be of much help to you.

Now start making index.php. Initially, there are three articles in index.php. When you open index.php, you can see the three tags of the article d699a3398de91626f8da48ef90fa2610. Now we will delete the codes of the two articles, leaving one article and remove the article abstract. The modified code is like this:

578c83d58b4fb2f59eaf1658e68c7eb3
	bf0a7784259ea2450e04ad73e4261b25
	c816fb4c82c833170a1e3d36de6e495e
		b9dd13fe880cb5d97a29da82b97a7677
		7f51ef8274e570298e54b11fdb1f5e16
			ac3a27832e918005473c47c694ceca6a
			c3606beab6681aa5853aec0b47ea0fb570e0cb0b1f9ab5693c68765caafc6b4cLoreum ipsium massa cras phasellus5db79b134e9f6b82c0b36e0489ee08ed39528cedfa926ea0c01e69ef5b2ea9b0
			3702578b6fd3101b3a82621ffd3e7b4c
			40c373f4d56d7db9536913dd82742331f1b3ba8846c179e65eb68da95b01a812News5db79b134e9f6b82c0b36e0489ee08ed, f1b3ba8846c179e65eb68da95b01a812Products5db79b134e9f6b82c0b36e0489ee08ed • 31st Sep, 09 • f1b3ba8846c179e65eb68da95b01a8121 Comment5db79b134e9f6b82c0b36e0489ee08ed94b3e26ee717c64999d7867364b1b4a3
			e972da4d548fe480c40c9a2f1227ca04 16b28748ea4df4d9c2150843fecfba68
			c508d4854be103cfb4b7e0d9bcaf9372
			d86cddf8d691fdd46d642e760c40e628/images/610x150.gif" />
			e503568341a33083ea0ffa60142b17e3
			
			afc9b62199be7498111d94fe3268e34b
			2661fe612134550c8a43d6935cef3270528277898e8ea39ab50bce7f457fe13e Read More...5db79b134e9f6b82c0b36e0489ee08ed94b3e26ee717c64999d7867364b1b4a3
		16b28748ea4df4d9c2150843fecfba68
		1cc748e10901b6715fa9087abf370429 16b28748ea4df4d9c2150843fecfba68
		
		3f3875c94b33407c2adf38370fe28f7b
		2661fe612134550c8a43d6935cef3270 0c76242f8aa99df277e784671af8a1c1<< Previous Posts5db79b134e9f6b82c0b36e0489ee08ed f9076eaab5893c56dd7bd005bceb51f4Newer Posts >>5db79b134e9f6b82c0b36e0489ee08ed 94b3e26ee717c64999d7867364b1b4a3
	16b28748ea4df4d9c2150843fecfba68
	7e289a154b156d7d8b381b9812fc24759e8f83f9df7fbb8d080845d182c4518b
As you can see from the above code, the html skeleton of an article is:
<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(&#39;template_url&#39;); ?>/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>

The html skeleton of articles with different themes is different. If you are familiar with html, You can quickly identify the skeleton of the article. The above is the skeleton of our theme. Based on this, we will add dynamic content to index.php:

1. Add article title

Found:

c3606beab6681aa5853aec0b47ea0fb570e0cb0b1f9ab5693c68765caafc6b4cLoreum ipsium massa cras phasellus5db79b134e9f6b82c0b36e0489ee08ed39528cedfa926ea0c01e69ef5b2ea9b0

Changed to:

c3606beab6681aa5853aec0b47ea0fb51c81c5c4beb08a8ee9c9610b837ee9f2" rel="bookmark">7c451a7bd609eaed8c011cfb968e93295db79b134e9f6b82c0b36e0489ee08ed39528cedfa926ea0c01e69ef5b2ea9b0

Here are the explanations of these php functions:

  • be582d88f00d4e9080d24b7280839fbe Output the URL link of the article
  • 2dc51b38c99501b394e46960b1866842 Output the title of the article

2. Add article tags

Many of us like to add some tags when writing articles, and we have also added a "tag cloud" to the sidebar, our theme Tags should be supported. Found:

f1b3ba8846c179e65eb68da95b01a812News5db79b134e9f6b82c0b36e0489ee08ed, f1b3ba8846c179e65eb68da95b01a812Products5db79b134e9f6b82c0b36e0489ee08ed

Changed to:

987ba55c46b6e0200abe3b0119813994

Function reference: the_tags

3. Add date

Found: 31st Sep, 09

Changed to:

5bc7549643812eee01cfb2270039a293

Function reference: the_time

About Y in this function For the date format obtained by n j, you can refer to the document (Chinese) and choose the time format you like: zh-cn: Custom time and date

Maybe you have read the time and date documents provided above, or If you are confused, here are a few examples. You can almost follow the example and specify the time and date format you like:

##db6d98a9d1d7a89ef7f37949587ab43d99-05-01 02:09:08

4、显示评论数

现在我们来添加评论数代码,查找代码:

f1b3ba8846c179e65eb68da95b01a8121 Comment5db79b134e9f6b82c0b36e0489ee08ed

改成:

ef508cf2b924fde2477e8c01cdd25031

该函数会根据文章的评论数量显示不同的文字链接,0 条评论、1 条评论等等,当然能你可以根据自己的爱好定制文字内容。该链接会直接打开对应的文章,并移动到文章的评论区.

函数参考:comments_popup_link

5、添加编辑按钮

如果文章作者已登录,我们将允许他在首页点击对应文章的编辑按钮,就可以直接修改文章,这个功能是可选的,你可以不添加。接上面的评论按钮,我们在其后面添加相应代码:

ef508cf2b924fde2477e8c01cdd250310400cd0a04d7793670cf7e6c0900e9ae

函数参考:edit_post_link

6、添加文章内容

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

将其改成:

e503568341a33083ea0ffa60142b17e3
dfe5ee8d7f498071d6bfae5e746ca35e

只要在写文章的时候在"摘要"框内填写摘要,在首页显示的就是摘要,如果不填就输出全文!以下是方案二,用于输出全文,除非你在文章中使用了95712e0b0bc62c64b1fddfac486f9cb8,代码修改:

e503568341a33083ea0ffa60142b17e3
6ebeb6c2a8e770d3f84b91f0543c9ddf

函数参考:

  • the_excerpt
  • the_content

7、阅读全文

这里给添加阅读全文链接,如果在6、添加文章内容中你选择了输出全文,本步骤可以忽略,查找代码:

528277898e8ea39ab50bce7f457fe13e Read More...5db79b134e9f6b82c0b36e0489ee08ed

改成:

1c81c5c4beb08a8ee9c9610b837ee9f2" class="button right">阅读全文5db79b134e9f6b82c0b36e0489ee08ed

8、添加文章循环

到目前为止,你的首页还只能显示一篇文章,要想输出所有文章,需要我们之前提到的循环。查找代码:

d699a3398de91626f8da48ef90fa2610

改成:

b9dd13fe880cb5d97a29da82b97a7677f5fccccc1831b51a0cd3bd7d13525eb3

再查找:

1cc748e10901b6715fa9087abf370429 16b28748ea4df4d9c2150843fecfba68

改成:

1cc748e10901b6715fa9087abf370429 16b28748ea4df4d9c2150843fecfba68adb6314416ae9ef0e5a3270d39150de9

再次查找:

16b28748ea4df4d9c2150843fecfba687e289a154b156d7d8b381b9812fc2475

改成:

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

好了,现在查看你的主页,是不是可以显示多篇文章了呢?文章数量取决于你在后台设置每页可显示的文章数量。以上的循环可以简化为以下内容,这样看起来应该比较容易理解了,在endwhile之前不断地输出每篇文章,直至文章数量达到每页显示的最大文章数量;如果你的博客上一篇文章都没有,就会输入无文章提示。

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
文章html骨架
<?php endwhile; ?>
<?php else : ?>
输出找不到文章提示
<?php endif; ?>

9、添加文章分页

上面你已经看到,每页只能显示部分文章,要想看下一页,就得添加分页。现在查找代码:

2661fe612134550c8a43d6935cef3270 0c76242f8aa99df277e784671af8a1c1<< Previous Posts5db79b134e9f6b82c0b36e0489ee08ed f9076eaab5893c56dd7bd005bceb51f4Newer Posts >>5db79b134e9f6b82c0b36e0489ee08ed 94b3e26ee717c64999d7867364b1b4a3

改成:

2661fe612134550c8a43d6935cef3270294e9457a6e2425c2758afc8200b6efa 6936de35a46d7dbf75038ddb2436e510dde75106bda5d6f031c7ad0837cfc772>', 0); ?>54bdf357c58b8a65c66d7c19c8e4d11494b3e26ee717c64999d7867364b1b4a3

参考函数:

  • previous_posts_link
  • next_posts_link

10、文章缩略图

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

c508d4854be103cfb4b7e0d9bcaf9372d86cddf8d691fdd46d642e760c40e628/images/610x150.gif" />

另外,还有个存档页面的模板archive.php,跟index.php的制作过程完全一样,只不过需要在functions.php里添加一个函数,这里就不再罗嗦,下载自己看吧,注意:functions.php中的代码已经修改,要想让你的分类、标签等存档页能够正常显示,请下载最新的functions.php覆盖原来的。另外,标签页和分类页支持在该页面顶部显示介绍,前提是你在后台文章标签和分类处要填上了描述。

好了,到目前这个主题也像个样子了,不过还有很多要完善,后面我们将继续完善!

推荐学习:《WordPress教程

PHP code Output content
91400d1358fa63cfdf75953fea959c64 May 1, 1999
366fa87a03157247c8e919603be2b26e May 01, 1999
a9ca9f1e62c36239d38c2a21850f950c 1999-05-01

The above is the detailed content of The whole process of WordPress theme creation (8): making index.php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:ludou.org. If there is any infringement, please contact admin@php.cn delete