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.


Nothing (null).



Basic Usage

if ( have_posts() ) {
	while ( have_posts() ) {


		<h2><?php the_title(); ?></h2>



#1 How to use a function in the WordPress Loop

while( have_posts() ){

	echo '<h2>';
	echo '</h2>';

#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 5.1.1

function the_post() {
	global $wp_query;

Related Functions

From tag: WP Loop

More from category: Queries

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

Tatya 100
No comments
    Hello, !     Log In . Register