Wordpress action loop

Source: Internet
Author: User
Tags pings
Preface & quot; loop & quot; is a term that specifies the main process of WordPress. You apply the loop in your template file to present your article to the reader. You can create templates that do not contain loops, but you can only display the data of one article. Before we enter 'cyclical', let's take a look at the background of WordPress's actions before the loop starts. The First Preface

"Loop" is a term that specifies the main program process of WordPress. You apply the loop in your template file to present your article to the reader. You can create templates that do not contain loops, but you can only display the data of one article.

Before we enter 'cyclical', let's take a look at the background of WordPress's actions before the loop starts. The first thing it needs to do is check all the files it needs to provide. It then collects the default settings of the blog administrator from the database. This includes the number of articles displayed on each page, whether the comments are loaded, and other things. Once the default data volume is set up, WordPress checks the user request content. This information will be used to determine which articles will be crawled from the database.

If the user does not request specific articles, categories, pages or data, WordPress uses the default data collected in advance to decide which articles are provided to the user. For example, if the blog administrator is already managing?> Set?>? Read settings to display 5 articles on each page, WordPress will capture the latest 5 articles from the database to the user. If a user requests a specific article, category, page, or data, WordPress uses this information to determine which data is retrieved from the database.

Once these processes are completed, WP connects to the database, retrieves specific information, and stores a variable in the result. Loop needs to call this variable to control your template output.

By default, if a visitor does not select a specific article, page, category, or data, WP usesIndex. phpTo display everything. As the first part of the discussion loop, we will focus onIndex. phpAnd output your article by default. later, as long as you understand how the work is completed, we will introduce the loop technology under other template files.

The simplest index page in the world

The following shows a full-featured homepage File (index. php). it only displays the content of each article, and uses it to adjust the cycle as needed. The purpose of this demonstration is to prove how simple a loop is. MostIndex. php? More css, html, and php are added to the loop, which makes the loop look bigger and more beautiful.


Now let's do something to make the loop look more beautiful!

Default loop

Let's take a step-by-step look at it?Default? And?Classic? Based on the standard WordPress v1.5.

Start cycle

InIndex. phpAt the top of the file, you can see how the loop starts.

  1. FirstHave_posts ()Method to check whether there are articles.
  2. If there is an article, PHP?WhileThe loop starts .?While? The loop continues until the conditions in the brackets are true. SoHave_posts ()Returns true,WhileThe loop will not stop (Have_posts ()? The method simply checks whether the next article can be found. If yes,IfJudge whether the returned result is true,WhileThe loop is executed again. if there is no next article,IfReturns false, jumps out of the loop ).
Produce articles

The_post ()Method to get the current post in the posts set and make it effective in the loop iterator. no?The_post (), Most? Template tag? Cannot be used.

When the document information is available, the template file displays the document information to visitors.

Title, date, and author

The following template tag? The title, time, and author of the current article are obtained.

">" Rel = "bookmark" title = "Permanent Link

Article content

The_content ()Is the content of the article. It is the "meat" of the article in the loop ".


If you are familiar with CSS, note thatDiv? Assigned?Class = "entry"In this way, you can set the style or function based on the specific symbol.

More labels? : If the article contains a quick tag? Called?More, Write? , All the previous ones will be displayed in the loop, and the subsequent ones will be omitted.

Independent article page? ? Will be ignored. So use? ? The reader can be forced to enter the separate article page.

Other details

Beneath each post's content in?Index. php? Template file is a place to display more information about the post, such as the categories, date, and comment information. Known as? Post meta data section, if you're a logged in user of sufficient privilege (or the post's author), you will also see an "Edit This" link, thanks to?Edit_post_link ()? Template tag function.

Posted in | |');??>

If commenting is enabled, or if the post has comments,?Comments_popup_link ()? Template tag will display a link to the comments. If you're using? Comments popup window, this link will open the comments window; otherwise it will jump right to this post's comments.

If the visitor is viewing an index of posts (I. e .:? More than one post in The Loop),?Comments_popup_link ()Link will take the reader to this post's individual page.

Automatic Trackback detection

The?Trackback_rdf? Template tag's function is to output machine-readable code used? Trackback? Auto-discovery.


Note:? The?Trackback_rdf ()? Tag is supposed to be used? Comments? Around it. It is not "turned off ".

End loop

The following ends The Loop. after this, the various post-related template tags will not work as expected (or if they do, they will use the last post from The Loop ). this means, that if you need to use a template tag that works?Within The Loop, You need to put it in before this point.


This section, immediately after the end of The Loop, displays navigation controls to move forward and backward by each web page. More information is available in function reference? Posts_nav_link ().


If the blog is set to display 10 posts per page, and the conditions used by The Loop collect 25 posts, there will be three pages to navigate: two pages of 10 posts each, and one page of 5 posts. the navigation links will allow the visitor to move forward and backward through the collection of posts.

The navigation controls are supported ded?Outside? The Loop,?Inside? The?If? Condition, so that they only show up if there are any posts. the navigation functions themselves also check whether or not there is anything to which they will link, based on the current Loop, and only display links if there's something to link.

  Not Found 

The?Else? :? Clause determines what to do if?Have_posts ()? (From way up at the top) is false. That is to say, the stuff after?Else? Will only be executed/displayed if The Loop had zero posts. no posts show up if, for example, the visitor requested a specific day for which no posts were made or a search was saved med that produced no results.


This ends the conditional test of "if there are posts do this, else if there are no posts, do that ". once the conditional test is finished, the default index. php template next includes des the sidebar, and finally the footer.

Loops in other templates

WordPress uses different template files to display blogs in a variety of ways. In the default WordPress topic, use? Template files? As a template for displaying individual articles. ? The Loop template, but uses a slightly different style, refer? Different template tags usage.

For any view which does not have a separate template file, WordPress will use?Index. php? By default. If a visitor requests a single post, WordPress will first look for a file named?Single. php. If that file exists, it will be used to present the post to the visitor. If that file does not exist, WordPress will use?Index. php? To present the post to the visitor. This is called? Template Hierarchy.

If you are making your own? Theme, it's often helpful to look at? Template files? From the default Theme as a point of reference. It's also helpful to use your theme's?Index. php? As a template for your other template files. Doing so may give you a known and working page from which to begin making changes as you create more template files.

Different archive formats

An?Archive? Is a collection of historical posts. In the default usage, the posts displayed on your main index are recent? Chronological? Postings. When a visitor clicks on one of your archive links, or if they manually request a specific date (http://www.example.com/blog/index.php? M = 200504 or http://www.example.com/blog/2005/04 to select all posts from sort L, 2005), WordPress will display?Archive? View. By default, the archive will use?Index. php, And thus look the same as your front page, just displaying the posts from limit L 2005.

When WordPress prepares? Archive view? For a visitor, it specifically looks for a file named?Archive. php? In your current theme's directory. If you 'd like to communicate ally disambiguate archives from your front page, simply copy?Index. php? To?Archive. php, And edit?Archive. php? As necessary!

For example, if you want to show only post titles, and no post content, for your list of archives, you cocould use something like this:

           ">" rel="bookmark" title="Permanent Link to 
   Not Found 

Different classification formats

Like the archive views, WordPress looks for a separate template file? Category views. if a visitor clicks on a link for a category in your blog, they will be taken to the category view. wordPress will prepare The Loop with posts from that category only, limiting the number of posts per the blog's default settings.

To make your category view different from your index view, copy?Index. php? And rename it?Category. php. For a category view, it's probably not necessary to list the categories to which a post is assigned, so let's remove that portion. instead, let's announce the category at the top of the page:


">" rel="bookmark" title="Permanent Link to "> Not Found

Different formats of different categories

As explained in? Template Hierarchy, it is possible? Create separate template files for each category. Simply name the file?Category-X. Php, Where?X? Is the numerical ID of the category. Consider carefully whether you need a whole new template for a specific category.

Let's look at two categories, "Plants" and "Flowers", with category IDs 3 and 4, respectively. next to each post title in the output you want to have picture of either a plant, or a flower, depending on which category is being displayed. you cocould:

  • Use two separate files ,?Category-3.php? And?Category-4.php, Each with a different?Img? Tag for each post title.
  • Use a conditional test inside your default?Category. php? File to check whether the current category is "Plants" or "Flowers" (or neither), and display the appropriate image:

If you added another category, "Cars", which you wanted to display in?Significantly? Different way, then a separate?Category-X. Php? Wocould be more appropriate.

Different types of CSS styles

Extends users want to create separate CSS files for a specific category. This, too, can be easily accomplished. It is important to remember that stylesheets are defined and loaded in?? Section of the HTML document. WordPress uses?Header. php? File for this. In the default?Header. php, Find this line:


And change it to something like this:


Note:? The Cars template uses?Category-5.css? File to override the default layout. in this example the CSS file is named after the category template file to which it will be applied, rather than the actual name of the category. thus, you know that?Category-5.css? Goes?Category-5.php.

Different single article formats

When viewing any single post (or? Permalink), WordPress will use?Single. php, If present.

This portion, from the WordPress default single. php, provides? Post meta data information? About the current post:

This entry was posted on at and is filed under .You can follow any responses to this entry through the feed. comment_status) && ('open' == $post->ping_status)) {// Both Comments and Pings are open?> You can leave a response, or ">trackback from your own site. comment_status) && ('open' == $post->ping_status)) {// Only Pings are Open ?> Responses are currently closed, but you can ">trackback from your own site. comment_status) &&?!('open' == $post->ping_status)) { // Comments are open, Pings are not ?> You can skip to the end and leave a response. Pinging is currently not allowed. comment_status) &&?!('open' == $post->ping_status)) { // Neither Comments, nor Pings are open ?> Both comments and pings are currently closed.

This sort of information -- whether comments are open or closed -- is largely inappropriate on an index, archive, or category view; which is why it's only supported in?Single. php? Template file.

Other loop techniques

Now that you have a good introduction to the basic uses for the WordPress Loop, let's introduce you to some more Loop effects and tricks.

Static homepage

How to make some special contentOnlyDisplayed on the homepage? Yes, it is only displayed on the homepage, not any other page on your website. Very simple! This is calledStatic homepage. The homepage or the first page of your website is not really static, but appears like this through loops.

To do this, we use the condition template tagIs_home ().

In?Index. php, Use?If ()? Determine to output additional content selectively:


When a visitor does not request an article, page, category, or date, what is the function?Is_home ()? Returns true. The above content will only be displayed on the homepage.

For more information, see? Create a static homepage.

Show abstract only

The easiest way to display excerpts, instead of the full content, of posts, replace all instancesThe_content ()? With?The_excerpt (). If you have not created explicit excerpts for your posts, this function will automatically display the first 55 words of the post.


Displays the abstract or full text by document number

In some circumstances, for example on archive pages, you may want to show the full post if there is only one post or excerpts if there are multiple posts. you can customize the loop to do this.

 post_count) > 1)?:??>     

Different top and bottom columns

WordPress offers?Get_header (),?Get_sidebar (), And?Get_footer ()? Include Tags? For use in your? Template files. these functions make it easy to define a standard header/sidebar/footer which is easily editable. any changes made to these files will immediately be made visible to viewers, without any work on your part.

But sometimes you might not?Want? A sidebar. If you don't want a sidebar, simply exclude the call toGet_sidebar ()? Function from your template. For example,?Single. php? Template in the WordPress default theme does not include a sidebar.

To create your own?Different? Sidebar, you have two choices:

  1. Include the sidebar contents directly into the template file on which you're working. If you want category-3 to have a different sidebar, edit?Category-3.php? And include the necessary HTML and PHP to generate your distinctive sidebar.
  2. Use the PHP?Include? Function, to include another file. The WordPress?Get_sidebar ()? Function?Only? LoadsSidebar. php. If you make a file named?Sideleft. php, You wocould include it like this:

In WordPress? Version 2.5? And above you can also call a sidebar like this:


This causes the template TEMPLATEPATH. 'sidebar-right. php' to be supported ded.

Using the WordPress default? Template Hierarchy, if you want to use the same elements on multiple or different templates, it's probably best to put them in separate template files and use the PHP?Include ()? Function. If the element you're adding is specifically for one template file, it's probably best to include it directly in that template file.


We just got a general idea about how to deal with loops. Note: The following is a resource that helps you customize your own WordPress Loop.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.