This link has been bookmarked by 142 people . It was first bookmarked on 02 Mar 2006, by J Nale.
-
17 Sep 15
-
19 Mar 15
-
"The Loop" is the main process of WordPress. You use The Loop in your template files to show posts to visitors. You could make templates without The Loop, but you could only display data from one post.
-
Before The Loop goes into action, WordPress verifies that all the files it needs are present. Then it collects the default settings, as defined by the blog administrator, from the database. This includes the number of posts to display per page, whether commenting is enabled, and more. Once these defaults are established, WordPress checks to see what the user asked for. This information is used to determine which posts to fetch from the database.
-
If the user didn't ask for a specific post, category, page, or date, WordPress uses the previously collected default values to determine which posts to prepare for the user. For example, if the blog administrator has selected to display 5 posts per page in Administration > Settings > Reading, then WordPress will fetch the five most recent posts from the database. If the user did ask for a specific post, category, page, or date, then WordPress will use that information to specify which post(s) to fetch from the database.
-
<?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?>
- First, it checks whether any posts were discovered with the have_posts() function.
- If there were any posts, a PHP while loop is started. A while loop will continue to execute as long as the condition in the parenthesis is logically true. So, as long as the function have_posts() returns a true value, the while loop will keep looping (repeating).
-
The function the_post() takes the current item in the collection of posts and makes it available for use inside this iteration of The Loop. Without the_post(), many of the Template Tags used in your theme would not work.
Once the post data is made available, the template can start converting it into HTML to send to the visitor.
-
The the_content() template tag fetches the content of the post, filters it, and then displays it. This is the meat and potatoes of each pass through The Loop:
-
WordPress can use different template files for displaying your blog in different ways. In the default WordPress theme, there are template files for the index view, category view, and archive view, as well as a template for viewing individual posts. Each of these uses The Loop, but does so with slightly different formatting, as well as different uses of the template tags.
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 the Template Hierarchy.
-
If you are making your own Theme, it's often helpful to look at the 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.
-
- 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.
- Use the PHP include function, to include another file. The WordPress get_sidebar() function only loads sidebar.php. If you make a file named sideleft.php, you would include it like this:
But sometimes you might not want a sidebar. If you don't want a sidebar, simply exclude the call to the get_sidebar() function from your template. For example, the single.php template in the WordPress default theme does not include a sidebar.
To create your own different sidebar, you have two choices:
<?php include(TEMPLATEPATH . '/sideleft.php'); ?>
In WordPress Version 2.5 and above you can also call a sidebar like this:
<?php get_sidebar('right'); ?>This causes the template TEMPLATEPATH . 'sidebar-right.php' to be included.
-
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.
-
-
06 Nov 14
-
while (have_posts()) : the_post(); the_content(); endwhile;
-
<?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?>
-
The function the_post() takes the current item in the collection of posts and makes it available for use inside this iteration of The Loop
-
-
02 Oct 14
-
If the user didn't ask for a specific post, category, page, or date, WordPress uses the previously collected default values to determine which posts to prepare for the user.
-
By default, if the visitor did not select a specific post, page, category, or date, WordPress uses index.php to display everything.
-
The function the_post() takes the current item in the collection of posts and makes it available for use inside this iteration of The Loop.
-
-
24 Aug 14
-
04 Aug 14
-
You could make templates without The Loop, but you could only display data from one post.
-
if (have_posts())
-
while (have_posts())
-
-
04 Apr 14
-
25 Mar 14
-
You use The Loop in your template files to show posts to visitors.
-
main process
-
then WordPress will use that information to specify which post(s) to fetch from the database
-
WordPress uses index.php to display everything.
-
(The function have_posts() simply checks for a next item in the possible collection of posts.
-
<?php endwhile; ?>
-
WordPress can use different template files for displaying your blog in different ways.
-
Different Archive Format
-
Different Category Format
-
Different Formats for Different Categories
-
Different CSS For Different Categories
-
Different Single Post Format
-
basic uses for the WordPress Loop
-
Different Headers/Sidebars/Footers
-
scratched the surface of what can be done with the Loop.
-
-
25 Jan 14
-
02 Nov 13
-
if the visitor did not select a specific post, page, category, or date, WordPress uses index.php to display everything
-
-
05 Aug 13
-
26 May 13
-
10 Apr 13
travisndiesingShows how The Loop is used in wordpress to call and display multiple posts.
wordpress plugin the loop php development code blogging webdesign documentation
-
09 Sep 12
-
03 Jun 12
-
14 May 12
-
02 Feb 12
-
The navigation controls are included outside The Loop, but inside the if condition, so that they only show up if there are any posts
-
That is to say, the stuff after the else will only be executed/displayed if The Loop had zero posts.
-
-
15 Dec 11
-
03 Nov 11
-
23 Oct 11
-
"The Loop" is a term that refers to the main process of WordPress. You use The Loop in your template files to show posts to visitors
-
By default, if the visitor did not select a specific post, page, category, or date, WordPress uses index.php to display everything.
-
The function have_posts() simply checks the next item in the collection of posts: if there's another item, return true; if there is no next item, return false.
-
The function the_post() takes the current item in the collection of posts and makes it available for use inside this iteration of The Loop.
-
The Loop In Other Templates
WordPress can use different template files for displaying your blog in different ways.
-
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 the Template Hierarchy.
-
If you are making your own Theme, it's often helpful to look at the 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 Format
An archive is a collection of historical posts.
-
When a visitor clicks on one of your archive links, or if they manually request a specific date
-
WordPress will display an archive view. By default, the archive will use index.php, and thus look the same as your front page, just displaying the posts from April 2005.
-
When WordPress prepares an 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 visually disambiguate archives from your front page, simply copy index.php to archive.php, and edit archive.php as necessary!
-
Different Category Format
Like the archive views, WordPress looks for a separate template file for 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
-
Different Formats for Different Categories
As explained in the Template Hierarchy, it is possible to 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.
-
Different CSS For Different Categories
Many 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 the <head> section of the HTML document. WordPress uses the header.php file for this. In the default header.php
-
And change it to something like this:
<?php if ( is_category('5') ) { // Load special CSS for "Cars" category ?> <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/category-5.css" type="text/css" media="screen" />; <?php } else { ?> <link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" /> <?php } ?> -
Different Single Post Format
When viewing any single post (or permalink), WordPress will use single.php, if present.
-
Showing Excerpts or Full Post Depending Upon Number of Posts
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.
-
-
17 Aug 11
-
You use The Loop in your template files to show posts to visitors
-
check that all the files it needs are present
-
collects the default settings
-
-
30 May 11
-
29 May 11
-
10 May 11
-
30 Apr 11
-
07 Mar 11
-
03 Mar 11
-
05 Feb 11
-
The first thing WordPress does is check that all the files it needs are present. Next, it collects the default settings, as defined by the blog administrator, from the database. This includes things like the number of posts to display per page, whether commenting is enabled, and the like. Once these defaults are established, WordPress checks to see what the user asked for. This information is used to determine which posts to fetch from the database
-
-
21 Jan 11
Casson McRaeShows hoe to display just title and as a link
-
14 Jan 11
-
03 Jan 11
-
Showing Excerpts or Full Post Depending Upon Number of Posts
-
WordPress connects to the database, retrieves the specified information, and stores the results in a variable. It is The Loop that accesses this variable, and uses the values for display in your templates.
-
Showing Excerpts or Full Post Depending Upon Number of Posts
-
If you include the Quicktag button called more, and shown as <!--more-->, in the body of your post, only the portion above that line will be displayed to viewers. So, if you only want your front page to show the first sentence or two of every post, simply insert <!--more--> after the first line into every post you make.
-
When viewing a single post, the <!--more--> delimiter is skipped. So putting the <!--more--> delimiter into all your posts forces readers to click through to each individual post if they want to read the whole thing.
-
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 included outside The Loop, but 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.
-
If you are making your own Theme, it's often helpful to look at the 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.
-
When WordPress prepares an 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 visually 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 could use something like this:
-
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:
-
As explained in the Template Hierarchy, it is possible to 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.
-
- 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:
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 could:
-
If you added another category, "Cars", which you wanted to display in a significantly different way, then a separate category-X.php would be more appropriate.
-
Many 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 the <head> section of the HTML document. WordPress uses the header.php file for this. In the default header.php, find this line:
-
Note: The Cars template uses the 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 with category-5.php.
-
When viewing any single post (or permalink), WordPress will use single.php, if present.
-
How can you display something special only on the front page of your blog? That's right, only on the front page or home page, and have it not be seen anywhere else on your site. Easy! We call this the static front page. The front or first page of your site isn't really static. It's just using the Loop to make it look that way.
-
To make this Loop trick work, use the is_home() conditional template tag function.
In your index.php, use an if () test to conditionally output additional content:
-
The easiest way to display excerpts, instead of the full content, of posts, replace all instances of the_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.
-
Showing Excerpts or Full Post Depending Upon Number of
-
Different Headers/Sidebars/Footers
-
To create your own different sidebar, you have two choices:
-
The WordPress get_sidebar() function only loads sidebar.php. If you make a file named sideleft.php, you would include it like this:
-
<?php include(TEMPLATEPATH . '/sideleft.php'); ?>
-
In Wordpress Version 2.5 and above you can also call a sidebar like this:
<?php get_sidebar('right'); ?> -
This causes the template TEMPLATEPATH . 'sidebar-right.php' to be included.
-
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've just scratched the surface of what can be done with the Loop. As a reminder, the following are resources that will help you customize your own WordPress Loop.
-
-
01 Jan 11
-
Begin The Loop
-
-
07 Dec 10
-
07 Nov 10
-
28 Oct 10
-
"The Loop" is a term that refers to the main process of WordPress. You use The Loop in your template files to show posts to visitors. You could make templates without The Loop, but you'd only be able to display data from one post.
-
The first thing WordPress does is check that all the files it needs are present. Next, it collects the default settings, as defined by the blog administrator, from the database.
-
If the user didn't ask for a specific post, category, page, or date, WordPress uses the previously collected default values to determine which posts to prepare for the user.
-
WordPress connects to the database, retrieves the specified information, and stores the results in a variable. It is The Loop that accesses this variable, and uses the values for display in your templates.
-
By default, if the visitor did not select a specific post, page, category, or date, WordPress uses index.php to display everything.
-
The Loop In Other Templates
WordPress can use different template files for displaying your blog in different ways. In the default WordPress theme, there are template files for the index view, category view, and archive view, as well as a template for viewing individual posts. Each of these uses The Loop, but does so with slightly different formatting, as well as different uses of the template tags.
-
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 the Template Hierarchy.
-
If you are making your own Theme, it's often helpful to look at the 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 Format
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 April, 2005), WordPress will display an archive view. By default, the archive will use index.php, and thus look the same as your front page, just displaying the posts from April 2005.
When WordPress prepares an 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 visually disambiguate archives from your front page, simply copy index.php to archive.php, and edit archive.php as necessary!
-
Different Category Format
Like the archive views, WordPress looks for a separate template file for 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:
-
- 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:
Different Formats for Different Categories
As explained in the Template Hierarchy, it is possible to 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 could:
-
Different CSS For Different Categories
Many 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 the <head> section of the HTML document. WordPress uses the header.php file for this. In the default header.php, find this line:
<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" />And change it to something like this:
<?php if ( is_category('5') ) { // Load special CSS for "Cars" category ?> <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/category-5.css" type="text/css" media="screen" />; <?php } else { ?> <link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" /> <?php } ?>Note: The Cars template uses the 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 with category-5.php.
-
Static Front Page
How can you display something special only on the front page of your blog? That's right, only on the front page or home page, and have it not be seen anywhere else on your site. Easy! We call this the static front page. The front or first page of your site isn't really static. It's just using the Loop to make it look that way.
To make this Loop trick work, use the is_home() conditional template tag function.
In your index.php, use an if () test to conditionally output additional content:
<?php get_header(); ?> <?php if (is_home()) { // we're on the home page, so let's show a picture of our new kitten! echo "<img src='/images/new_kitty.jpg' alt='Our new cat, Rufus!' />"; // and now back to our regularly scheduled home page } ?>The function is_home() will only produce a true value if the visitor is not requesting a specific post, page, category, or date, so it only shows up on the "home" page.
For more information, see Creating a Static Front Page.
-
Excerpts Only
The easiest way to display excerpts, instead of the full content, of posts, replace all instances of the_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.
<div class="entry"> <?php the_excerpt(); ?> </div>
-
- 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.
- Use the PHP include function, to include another file. The WordPress get_sidebar() function only loads sidebar.php. If you make a file named sideleft.php, you would include it like this:
Different Headers/Sidebars/Footers
WordPress offers the 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 to the get_sidebar() function from your template. For example, the single.php template in the WordPress default theme does not include a sidebar.
To create your own different sidebar, you have two choices:
<?php include(TEMPLATEPATH . '/sideleft.php'); ?>
-
-
03 Oct 10
Mare Parker-OToole"The following is a fully functional index which will display the contents (and just the contents) of each post, according to the conditions used to prepare The Loop. The only purpose for showing you this is to demonstrate how little is actually necessary for the functioning of The Loop. The bulk of the stuff in your index.php is CSS, HTML, and PHP declarations to make The Loop look pretty. "
-
07 Sep 10
-
06 Jun 10
-
You use The Loop in your template files to show posts to visitors.
-
display data from one post.
-
The World's Simplest Index Page
-
-
14 Apr 10
-
17 Mar 10
-
20 Feb 10
-
06 Feb 10
-
25 Jan 10
-
16 Dec 09
-
08 Dec 09
-
14 Nov 09
-
03 Nov 09
-
26 Oct 09
-
26 Aug 09
-
27 May 09
-
11 Apr 09
-
Without the_post(), many of the Template Tags used in your theme would not work.
-
-
31 Mar 09
-
16 Mar 09
Emily Brackett<?php get_header(); ?> <div id="content" class="narrowcolumn"> <p> <strong> <?php single_cat_title('Currently browsing '); ?> </strong><br /> <?php echo category_description(); ?> </p> <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post();
-
19 Jan 09
-
01 Dec 08
-
22 Nov 08
-
10 Nov 08
-
15 Oct 08
Gary Burgereviously, we saw how Wordpress can capably handle data more like a small-scale CMS than a traditional blog, assuming the developer is willing to get creative with some Template Tags and The Loop. With that in mind, let’s move toward a more sophisticated
-
18 Sep 08
-
29 Jul 08
-
27 May 08
-
04 May 08
-
02 May 08
-
11 Apr 08
-
25 Jan 08
-
14 Jan 08
-
11 Jan 08
-
28 Dec 07
-
13 Jun 07
-
14 Apr 07
-
09 Apr 07
Andre MalheiroThe first thing WordPress does is check that all the files it needs are present. Next, it collects the default settings, as defined by the blog administrator, from the database. Once these are established, WordPress checks to see what the user asked for.
-
12 Mar 07
-
"The Loop" is a term that refers to the main process of WordPress. You use The Loop in your template files to show posts to visitors.
-
-
03 Oct 06
-
10 Aug 06
-
07 Jul 06
-
17 Jun 06
-
07 May 06
-
30 Jan 06
-
21 Dec 05
-
08 Aug 05
-
19 Jul 05
Would you like to comment?
Join Diigo for a free account, or sign in if you are already a member.