Skip to main content
We are Brand SEO Beijing serving international business, your marketing partner, Contact us by

Website production theme production index.php file

We have extracted the public code in the index.php file and made it into header.php\footer.php\sidebar.php. Now let's sort out the code of index.php. The index.php file is one of the most commonly used If your theme does not have home.php and the home page is set to display the latest articles in the background, then the index.php file is the home page template. If your theme does not have an article template (single.php) and no single page template (page.php), no category page template (category.php), no tab page (index.php)... No 404 pages, etc., will use the index.php file instead.
So in what form should the index.php file we want to organize today be displayed? Generally speaking, it is a list of articles, so that this file can be displayed correctly as the home page, as an archive page, and a search result page. .
But what if we need to display 20 articles on our homepage?Do we need to write code for 20 articles?In fact, the codes of these 20 articles are all in the same form, so we only need to write the code of one article, and then output this code in a loop.If you have learned any programming language before, then the while\for loop should be familiar, as should the conditional if statement.
Open the index.php file with an editor below, you can see that there are three articles in the code, we will delete two of the codes, leaving only one, and delete the summary text:

Loreum ipsium massa cras phasellus News , Products • 1st Sep, 8 • 3 Comment /images/3x31.gif" /> Read More... << Previous Posts Newer Posts >>

We can see that, in fact, the skeleton of the article is the code framework that every article needs. Of course, this is only the theme here. If the style of the theme is different, the structure of the html code is also different. The code is as follows:

article titleLabel 3 , Label 3 • Post time • Number of comments /images/1x12.gif" />Article contentread full text button

Next, we will change the static content inside to dynamic.
The title of the article
Code the article title:

Loreum ipsium massa cras phasellus

replace with

" rel="bookmark">

The code inside the php function: the_permalink(); is the link address of the output current article, pay attention to the direct output; the_title(); function directly outputs the title of the current article.
XNUMX. Article tags
Put the tag code in index.php

News , Products

Replace with:

The_tags function directly outputs article tags.
Find the date text and replace 31st Sep, 09 directly with:

The_time function directly outputs the date of the article. As for the output format of Y year, n month, j day, you can also change it to Ynj. There are many parameters. Please check the official website yourself.
The number of comments
It seems to be very popular to display the number of comments and clicks of an article on the article archive page.

1 Comment

replace with

The three parameters in the comments_popup_link() function represent the output of no comments, one comment, and N comments, respectively. The % in it is equivalent to a placeholder.The code output by this function has a link that will link to the article page and locate the comment position.
XNUMX. Content of the article
Add code in place of article content

That is, in fact, what we want to output is the abstract, and the_content is the output of the article content, but on the home page and archive page, if you add the more tag to the article, it will output the content before the more tag, and add it after Previous "Read the full text" link.But many people will think of using another function the_excerpt() that outputs the abstract; I do not recommend you to do this, this function will output the abstract of the article (that is, when adding articles in the background, there is a place dedicated to adding abstracts), If there is no abstract, the first 50 characters will be automatically truncated.But this is for English, for Chinese multi-byte languages,This function cannot be intercepted, so it will output the full text. In comparison, is it more troublesome to add the More tag or more troublesome to fill in the abstract?However, if you plan to manually specify an abstract per article, it is recommended that you use the_excerpt function.
XNUMX. Article cycle
In the previous code, we have written the framework of an article, and now all we have to do is to put the framework code of this article in a loop statement to output.
In front of the article frame, there is a comment <!-- Blog Post. -->的地方,添加代码,效果:


After the article frame, add the code to end the loop and find:


change into


Find again:


change into:

not foundNo articles found! ?php get_sidebar(); ?>

OK, so far our loop code has been completed, analyze the code we just added, roughly like this:

Article html skeletonBeoutput not found article prompt

The have_posts() function is to determine whether there are currently articles: all the articles to be output on the current page are stored in a global array $posts, the have_post() function is to check a counter of this array, if there are currently articles, then return true, Return false if not;
The_post() function is used to advance the have_posts counter and fill the current article into the variable $post, while the previous functions the_title(), the_content() are only used to output the content in the $post variable, you completely usable


Instead of the_title() function, you can also output other content in the $post variable, such as the post ID.
Seven, article paging
The previous code can only output some articles at a time. If the entire blog has 100 articles, it is impossible to list all 100 articles. At this time, pagination is required.
Find our pagination code:

<< Previous Posts Newer Posts >>

Replace with:



Back to Top