After a few lectures in front of the study, we have learned the footer.php, header.php, style.css basic knowledge. Today to talk about the most important document index.php. In index.php the main thing is to remove the article-related information, which is the subject of the page information. We have mentioned in header.php this document, but also wrote two lines of code. Next, we rewrite the following code, and explain the use of the template function.
<?php get_header(); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class=”post” id=”post-<?php the_ID(); ?>”>
<h2><a class=”title” href=”<?php the_permalink() ?>” rel=”bookmark”><?php
<span class=”date”><?php the_time() ?></span>
<?php if ($options[‘author’]) : ?><span class=”author”><?php
the_author_posts_link(); ?></span><?php endif; ?>
<?php edit_post_link(‘edit’, ‘<span class=”editpost”>’, ‘</span>’); ?>
<span class=”comments”><?php comments_popup_link(‘no comment’,’1 comment’, ‘%
comments’, ‘Comments off’); ?></span>
<?php the_content(‘read more’); ?>
<span class=”categories”>Categories</span><span><?php the_category(‘, ‘); ?></span>
<span class=”tags”>tags: </span><span><?php the_tags(”, ‘, ‘, ”); ?></span>
<?php endwhile; else : ?>
<?php endif; ?>
<?php if(function_exists(‘wp_pagenavi’)) : ?>
<?php wp_pagenavi() ?>
<?php else : ?>
<span class=”newer”><?php previous_posts_link(‘上一页’); ?></span>
<span class=”older”><?php next_posts_link(‘下一页’); ?></span>
<?php endif; ?>
<?php get_footer() ?>
Please note that the quotation marks, which should be English half-width.
These codes are not some eye-candy, ah, it does not matter, we slowly to analysis.
（1）get_header (), get_footer () function
Remember the first two speakers we do header.php and footer.php file, the role of these two functions is the introduction of these two files, the equivalent of php in the include function, so that we can in the same page In the header and footer are displayed. This can also be seen the importance of index.php, he directs the other files. Similar to these two functions are get_siderbar () function, this function is to contain siderbar.php file, the contents of this file we will explain in the follow-up courses.
（2）have_posts (), the_post () function
Have_posts () role is to interpret the blog is not published articles, usually used for conditional judgment, the_post () role is to conduct a database query, the article related to the data query, so that we can in the follow-up code Using the relevant function of the information output to the page, the general usage is like the above:
Here is a loop to remove the article-related information
Here is if there is no article, then display the information here.
Note that if here and behind the use of a colon is not a semicolon, if mistakenly written semicolon can not achieve the desired function friends.
（3）the_ID() the_permalink() the_title() the_time() the_content(‘read more’)
Note: These five functions can only be used in WordPress loops, they are not global functions.
The role is to obtain a permanent link to the article. This function takes no arguments to get a link to an article in the current loop, or you can pass in an id number so that you can get a link to the article for that id. Functions are defined in:
/wordpress/wp-includes/link-template.php -> line 9
Get the title of the article, the function of the complete list of parameters the_title ($ before = “, $ after =”, $ echo = true)
$ Before refers to the title of the article in front of the content to be displayed, $ after refers to the title of the article said after the content to be displayed.
$ Echo true value that shows the title of the article, to return to false article title. The function is defined in:
/latest/wordpress/wp-includes/post-template.php -> line 35
The_time () is used to get and display the current article published time, the function can be followed with the control of the date or time format parameters, commonly used parameters are as follows:
If <? Php the_time ( ‘F j, Y’);?> Call the form of display: June 13, 2009 (English state display June 13, 2009);
If <? Php the_time ( ‘g: i a’);?> Call the form of display: 7: 09 pm (English state display 7:09 pm);
Such as <? Php the_time ( ‘G: i’);?> Calling the form of the show: 19: 09.
In fact, in addition to using the_time () function, WordPress also provides a similar function
The get_the_time () function. The function does not have the display function of the_time () function, the other functions are identical. use
Get_the_time () function, if you want to get the time displayed, you need to use a dedicated statement.
Here, we take this opportunity to briefly understand the time format in WordPress. In WordPress, the following characters are usually used to control the time format:
L, F, j, S, Y, G, g, i, a and so on, the detailed meaning is as follows:
L (lowercase L) is used to display the name of each day of the week, such as Saturday, or in English show Saturday;
F is used to display month names, such as June, or June;
J used to display a day in January, such as 13;
Y is used to display the year as a four-digit number, y for the last two digits, such as 2009 or 09;
G, g, i, a and other four characters are usually used in combination, as in the previous example, there are two forms:
G: i a displays the time in the form of 7:09 pm or 7:09 pm;
G: i displays the time in the form of a 24-hour period in the form of 19:09.
S is usually followed by j to indicate whether an English suffix (st, nd, th, etc.) is added after the day of January.
The contents of the article, this can take a parameter, the_content ( “more”), the role is in the article if <! – more->, it automatically truncated display more connections, you can view the details.
（4）the_category (), the_tags () function
The_category () to obtain the list of articles belonging to the article, you can use the_category ( ‘|’), if we have a WordPress WordPress theme WordPress plug-ins then the three directory is displayed like this:
WordPress Theme | WordPress Plugin, ‘|’ is the delimiter between adjacent directories, which can be separated according to their own needs using different symbols, by default, separated by spaces. The_tags () Gets the label the article belongs to. $ Before = The content displayed before the tag, the $ sep separator, and the content after the $ after tag. $ Before = $ tag = ‘, $ sep =’;