This link has been bookmarked by 259 people . It was first bookmarked on 29 Jul 2006, by Myer.
-
17 Sep 15
-
19 Jul 15
-
<?php $my_query = new WP_Query( 'category_name=featured&posts_per_page=2' ); while ( $my_query->have_posts() ) : $my_query->the_post(); $do_not_duplicate[] = $post->ID; ?>
-
-
19 Mar 15
-
- Title (the_title())
- Time (the_time())
- Categories (the_category()).
The Loop is PHP code used by WordPress to display posts. Using The Loop, WordPress processes each post to be displayed on the current page, and formats it according to how it matches specified criteria within The Loop tags. Any HTML or PHP code in the Loop will be processed on each post.
When WordPress documentation says "This tag must be within The Loop", such as for specific Template Tags or plugins, the tag will be repeated for each post. For example, The Loop displays the following information by default for each post:
You can display other information about each post using the appropriate Template Tags
-
-
- Title (the_title())
- Time (the_time())
- Categories (the_category()).
When WordPress documentation says "This tag must be within The Loop", such as for specific Template Tags or plugins, the tag will be repeated for each post. For example, The Loop displays the following information by default for each post:
You can display other information about each post using 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 beginner's look at The Loop, see The Loop in Action.
-
-
21 Feb 15
-
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
and ends here:
<?php endwhile; else : ?> <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p> <?php endif; ?>
-
-
06 Nov 14
-
The Loop is PHP code used by WordPress to display posts
-
-
17 Sep 14
-
-
02 Apr 14
-
25 Mar 14
-
WordPress processes each post to be displayed on the current page
-
The Loop is PHP code used by WordPress to display posts
-
The Loop should be placed in index.php
-
in any other Templates
-
-
11 Dec 13
-
The have_posts() and the_post() are convenience wrappers around the global $wp_query object, which is where all of the action is.
-
-
15 Nov 13
-
Exclude Posts From Some Category
-
Get the last 10 posts in the special_cat category.
-
If you need to keep the original query around, you can create a new query object.
-
In PHP 5, objects are referenced with the "= clone" operator
-
$do_not_duplicate equal to the ID number of the single post
-
if( $post->ID == $do_not_duplicate ) continue;?>
-
posts_per_page=2
-
$do_not_duplicate[] = $post->ID ?>
-
if (in_array($post->ID, $do_not_duplicate)) continue;
-
query_posts(array('post__not_in'=>$do_not_duplicate));
-
-
29 Sep 13
-
07 Aug 13
-
05 Aug 13
-
22 Jul 13
-
07 Jun 13
-
04 Jun 13
-
10 May 13
-
19 Apr 13
-
04 Mar 13
-
26 Feb 13
-
21 Feb 13
-
09 Sep 12
-
29 Jun 12
-
05 May 12
-
The loop starts here:
-
and ends here:
-
-
15 Apr 12
-
05 Mar 12
-
10 Feb 12
-
04 Jan 12
Tim Geers@Timgearz Als je tijd over hebt... http://t.co/7VF4hg7D
-
21 Nov 11
-
<?php rewind_posts(); ?>
-
<?php $my_query = new WP_Query('category_name=special_cat&posts_per_page=10'); ?>
-
In PHP 5, objects are referenced with the "= clone" operator instead of "=" like in PHP 4.
-
<?php $my_query = new WP_Query('category_name=featured&posts_per_page=2'); while ($my_query->have_posts()) : $my_query->the_post(); $do_not_duplicate[] = $post->ID ?>
-
<?php if (have_posts()) : while (have_posts()) : the_post(); if (in_array($post->ID, $do_not_duplicate)) continue; ?>
-
wp_reset_postdata();
-
-
12 Nov 11
-
used by WordPress to display each of your posts. Using The Loop, WordPress processes each of the posts to be displayed on the c
-
-
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.
-
-
29 Oct 11
-
<?php $my_query = new WP_Query('category_name=featured&posts_per_page=1'); while ($my_query->have_posts()) : $my_query->the_post(); $do_not_duplicate = $post->ID; ?> <!-- Do stuff... --> <?php endwhile; ?>
-
-
27 Sep 11
-
07 Aug 11
-
18 Jul 11
-
05 Jul 11
-
21 Jun 11
-
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.
-
-
31 May 11
-
20 May 11
-
10 Apr 11
-
25 Mar 11
-
The Loop displays by default: the Title (the_title()), Time (the_time()), and Categories (the_category()) for each post
-
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
-
<?php endwhile; else: ?> <p><?php _e('Sorry, no posts matched your criteria.'); ?></p> <?php endif; ?>
-
12 Mar 11
-
07 Mar 11
-
01 Feb 11
Wesley FryerThe loop is the core element of Wordpress for displaying post content
-
14 Jan 11
-
The Loop
-
display each of your posts
-
processes each of the posts to be displayed
-
each of the posts to be displayed on the current page
-
The Loop tags
-
will be repeated on each post
-
will be repeated on each post
-
"This tag must be within The Loop
-
This tag must be within The Loop
-
-
11 Jan 11
-
14 Dec 10
-
12 Dec 10
-
05 Dec 10
-
07 Nov 10
-
06 Nov 10
-
28 Oct 10
-
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.
-
Style Posts From Some Category Differently
-
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.
-
Note: Anytime you want to use HTML code, you must be outside the <?php ?> tags. PHP code (even things as simple as curly braces: } ) must be inside the <?php ?> 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.
-
Take a look at the basic Loop. It consists of:
<?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <!-- do stuff ... --> <?php endwhile; ?> <?php endif; ?>
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 <!-- do stuff ... -->. When you hit the last post, stop. The do stuff line(s), are template dependent.
-
Multiple Loops in Action
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.
-
-
13 Oct 10
-
04 Oct 10
-
21 Sep 10
-
11 Sep 10
-
09 Sep 10
-
25 Aug 10
-
22 Aug 10
-
12 Aug 10
-
04 Aug 10
Alessandro Muraro<!-- Display the Title as a link to the Post's permalink. -->
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<!-- Display the date (November 16th, 2009 form -
14 Jul 10
-
03 Jul 10
-
14 Jun 10
-
02 Jun 10
-
01 Jun 10
-
18 May 10
-
16 May 10
-
22 Apr 10
-
16 Apr 10
-
14 Apr 10
-
07 Apr 10
Charles GnilkaThe 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 co
-
11 Mar 10
-
20 Feb 10
-
06 Feb 10
-
25 Jan 10
-
09 Jan 10
-
30 Dec 09
-
18 Dec 09
-
08 Dec 09
-
30 Nov 09
-
03 Nov 09
-
27 Oct 09
-
01 Oct 09
-
23 Sep 09
-
22 Sep 09
-
19 Sep 09
-
16 Sep 09
-
04 Sep 09
monkey myndNote 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 -
13 Aug 09
-
05 Aug 09
-
25 Jul 09
-
12 Jun 09
-
20 May 09
-
12 May 09
-
09 May 09
-
23 Apr 09
-
22 Apr 09
Would you like to comment?
Join Diigo for a free account, or sign in if you are already a member.