the_post() WP 1.5.0
Iterate the post index in the loop. Gets the next post, overrides the global variable $post, and sets in the loop
property to true
.
Function the_post() checks whether the loop has started and then sets the current post by moving, each time, to the next post in the queue.
1 time = 0.000023s = very fast | 50000 times = 0.03s = speed of light | PHP 7.1.2, WP 4.7.3
No Hooks.
Return
Null. Nothing.
Usage
the_post();
Basic Usage
if ( have_posts() ) { while ( have_posts() ) { the_post(); ?> <h2><?php the_title(); ?></h2> <?php the_content(); } }
Examples
#1 How to use a function in the WordPress Loop
while( have_posts() ){ the_post(); echo '<h2>'; the_title(); echo '</h2>'; the_content(); }
#2 How to use a function outside the WordPress Loop
It is not necessary to use the Loop on single post page like is_single() if (have_posts ()) { while (have_posts ()) ) {the_post ()) ...
. On these pages, the $post variable is responsible for one post, so part of the loop can be taken away:
<?php the_post(); ?> <h1><?php the_title() ?></h1> <?php the_content(); ?>
Notes
- Global. WP_Query. $wp_query WordPress Query object.
Changelog
Since 1.5.0 | Introduced. |
Code of the_post() the post WP 5.6
function the_post() {
global $wp_query;
$wp_query->the_post();
}Related Functions
From tag: WP Loop
More from category: Queries
- get_page_by_title()
- get_post()
- get_posts()
- have_posts()
- in_the_loop()
- is_404()
- is_archive()
- is_attachment()
- is_author()
- is_category()
- is_date()
- is_day()
More from Template Tags: Posts, Pages, ...
- body_class()
- get_post_format()
- get_post_time()
- get_the_date()
- next_image_link()
- next_post_link()
- next_posts_link()
- post_class()
- post_password_required()