WordPress at a glance

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.

  • Global. WP_Query. $wp_query Global WP_Query instance.
Works based on: WP_Query::the_post()
✈ 1 time = 0.000023s = very fast | 50000 times = 0.03s = speed of light PHP 7.1.2, WP 4.7.3

No Hooks.

Return

Nothing (null).

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(); ?>

Code of the_post: wp-includes/query.php VER 4.9.8

<?php
function the_post() {
	global $wp_query;
	$wp_query->the_post();
}

Related Functions

From tag: WP Loop

More from category: Queries

More from Template Tags: Posts, Pages, ...

Tatya 100
No comments
    Hello, !     Log In . Register