The Loop is used by WordPress to display each of your posts. Using The Loop, WordPress processes each of the posts to be displayed on the current page and formats them according to how they match specified criteria within The Loop tags. Any HTML or PHP code placed in the Loop will be repeated on each post. When WordPress documentation states “This tag must be within The Loop”, such as for specific Template Tag or plugins, the tag will be repeated for each post.
For example, among the information The Loop displays by default in WordPress 1.5 are: the Title (the_title()), Time (the_time()), and Categories (the_category()) for each post. Other information about each post can be displayed with the appropriate Template Tags or (for advanced users) by accessing the $post variable, which is set with the current post’s information while The Loop is running.
ループへの初心者注視については、ループを活動して参照してください。
For a beginners look at The Loop, see The Loop in Action.
The Loop should be placed in index.php and in any other Templates used to display post information, but looks slightly different depending on your version of WordPress. You should first “find what version of WordPress you have”.
Be sure to include the call for the header Template at the top of your Theme’s Templates. If you are using The Loop inside your own design (and your own design is not a template), set WP_USE_THEMES to false.
Be sure to include the call for wp-blog-header.php at the top of your index page. Remember, the path for wp-blog-header.php must be set to the location of your wp-blog-header.php file:
This example, using the syntax for version 1.5, displays each post with its Title (which is used as a link to the Post’s Permalink), Categories, and Content. It is a simple, bare-bones example; likely your Templates will display more information in a way making things easier to style with CSS.
In order to be a little more instructive, though, this example also allows posts in a category with Category ID ‘3’ to be styled differently. To accomplish this, the in_category() Template Tag is used.
The tags are HTML comment tags; if you use this example, these tags will not display in web browsers. They serve no purpose other than to annotate the code below.
Note: Anytime you want to use HTML code, you must be outside the tags. PHP code (even things as simple as curly braces: } ) must be inside the tags. You can start and stop the PHP code in order to intersperse HTML code even within if and else statements, as shown in the above example.
あるカテゴリーからポストを除外します
Exclude Posts From Some Category
WordPress v1.5のために、そしてより高い
For WordPress v1.5 and higher
この例は表示からあるカテゴリーを除外するために使用することができます。それは上記の例に基づきます。
This example can be used to exclude a certain Category from being displayed. It is based on the example above.
Note: If you use this example for your main page, you should use a different Template for your Category archives. Otherwise, WordPress will exclude all posts in Category 3 even when viewing that Category Archive!
This will ensure that posts from Category 3 will only be excluded from the main page. There are other Conditional Tags that can be used to control the output depending on whether or not a particular condition is true with respect to the requested page.
Please note that even though the post is not being displayed it is still being counted by WordPress as having been shown — this means that if you have WordPress set to show at most seven posts and that two of the last seven are from Category 3 then you will only display five posts on your main page. If this is a problem for you, there is more complicated hack you can employ described in the Layout and Design FAQ or you can use query_posts if you only need to exclude one category from the loop.
This section deals with advanced use of The Loop. It’s a bit technical ? but don’t let that scare you. We’ll start off at easy and work up from there. With a little common sense, patience, and enthusiasm, you too can do multiple loops.
First off, “why would one want to use multiple loops?” In general, the answer is that you might want to do something with one group of posts, and do something different to another group of posts, but display both groups on the same page. Something could mean almost anything; you are only limited by your PHP skill and your imagination.
In English (PHP types and people familiar with code speak can skip to below), the above would be read: If we are going to be displaying posts, then get them, one at a time. For each post in the list, display it according to . When you hit the last post, stop. The do stuff line(s), are template dependent.
Aside on Do stuff: in this example it is simply a placeholder for a bunch of code that determines how to format and display each post on a page. This code can change depending on how you want your WordPress to look. If you look at the Kubrick theme’s index.php the do stuff section would be everything below:
An explanation for the coders out there: The have_posts() and the_post() are convenience wrappers around the global $wp_query object, which is where all of the action is. The $wp_query is called in the blog header and fed query arguments coming in through GET and PATH_INFO. The $wp_query takes the arguments and builds and executes a DB query that results in an array of posts. This array is stored in the object and also returned back to the blog header where it is stuffed into the global $posts array (for backward compatibility with old post loops).
Once WordPress has finished loading the blog header and is descending into the template, we arrive at our post Loop. The have_posts() simply calls into $wp_query->have_posts() which checks a loop counter to see if there are any posts left in the post array. And the_post() calls $wp_query->the_post() which advances the loop counter and sets up the global $post variable as well as all of the global post data. Once we have exhausted the loop, have_posts() will return false and we are done.
Below are two examples of using multiple loops. The key to using multiple loops is that $wp_query can only be called once. In order to get around this it is possible to re-use the query by calling rewind_posts() or by creating a new query object. This is covered in example 1. In example 2, using a variable to store the results of a query is covered. Example 3 documents the use of the update_post_caches(); function to avoid common plugin problems. Finally, ‘multiple loops in action’ brings a bunch of ideas together to document one way of using multiple loops to promote posts of a certain category on your blog’s homepage.
If you are finished with the posts in the original query, and you want to use a different query, you can reuse the $wp_query object by calling query_posts() and then looping back through. The query_posts() will perform a new query, build a new posts array, and reset the loop counter.
[php]
// Get the last 10 posts in the special_cat category.
[/php]
If you need to keep the original query around, you can create a new query object.
The query object my_query is used because you cannot use the global have_posts() and the_post() since they both use $wp_query. Instead, call into your new $my_query object.
Another version of using multiple Loops takes another tack for getting around the inability to use have_posts() and the_post(). To solve this, you need to store the original query in a variable, then re-assign it when with the other Loop. This way, you can use all the standard functions that rely on all the globals.
たとえば:
For example:
[php]
// going off on my own here
// now back to our regularly scheduled programming
[/php]
Note: In PHP 5, objects are referenced with the “=”-operator instead of copied like in php4. To make Example 2 work in PHP 5 you need to use the following code:
[php]
// going off on my own here
// now back to our regularly scheduled programming
[/php]
しかしながら、この第2の例はWordPress 2.1で働きません。
However, this second example does not work in WordPress 2.1.
It has been found that some plugins don’t play nice with multiple loops. In these cases it was discovered that some plugins which deal with the keyword(s) and tagging of posts, only work for the first instance of a loop in a page where that loop consists of a subset of total posts. If you find that this is the case, you might want to try the following implementation of the basic loop which adds the update_post_caches($posts) function. This function resets the post cache and is as yet undocumented. This implementation would be used on the second loop in a page only if the first loop retrieves a subset of posts.
The best way to understand how to use multiple loops is to actually show an example of its use. Perhaps the most common use of multiple loops is to show two (or more) lists of posts on one page. This is often done when a webmaster wants to feature not only the very latest post written, but also posts from a certain category.
Leaving all formatting and CSS issues aside, let us assume we want to have two lists of posts. One which would list the most recent posts (the standard 10 posts most recently added), and another which would contain only one post from the category ‘featured’. Posts in the ‘featured’ category should be shown first, followed by the second listing of posts (the standard). The catch is that no post should appear in both categories.
ステップ1
たった1つのポストを得る、から「特色とされた」カテゴリー。 Step 1. Get only one post from the ‘featured’ category.
Set $my_query equal to the result of querying all posts where the category is named featured and by the way, get me one post only. Also, set the variable $do_not_duplicate equal to the ID number of the single post returned. Recall that the Do stuff line represents all the formatting options associated for the post retrieved.
The following code gets X recent posts (as defined in WordPress preferences) save the one already displayed from the first loop and displays them according to Do stuff.
[php]
ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>
Get all posts, where a post equals $do_not_duplicate then just do nothing (continue), otherwise display all the other the posts according to Do stuff. Also, update the cache so the tagging and keyword plugins play nice. Recall, $do_not_duplicate variable contains the ID of the post already displayed.
最終結果
The End Result
ここに、最終のコードがフォーマットなしで見えるものがあります:
Here is what the final piece of code looks like without any formatting:
The end result would be a page with two lists. The first list contains only one post — the most recent post from the ‘feature’ category. The second list will contain X recent posts (as defined in WordPress preferences) except the post that is already shown in the first list. So, once the feature post is replaced with a new one, the previous feature will show up in standard post list section below (depending on how many posts you choose to display and on the post frequency). This technique (or similar) has been used by many in conjunction with knowledge of the Template Hierarchy to create a different look for home.php and index.php. See associated resources at the bottom of this page.
Note for Multiple Posts in the First Category
Note for Multiple Posts in the First Category
If showposts=2 or more, you will need to alter the code a bit. The variable $do_not_duplicate needs to be changed into an array as opposed to a single value. Otherwise, the first loop will finish and the variable $do_not_duplicate will equal only the id of the latest post. This will result in duplicated posts in the second loop. To fix the problem replace
This article on multiple loops is a combination of Ryan Boren and Alex King’s discussion about the Loop on the Hackers Mailing List as well as the tutorial written at MaxPower.
コメント