WordPress at a glance

in_the_loop() WP 2.0.0

Determines whether the caller is in The Loop (i.e. whether the loop is currently active). A conditional tag that is used to execute code only inside The Loop.

No Hooks.

Return

true/false. True if caller is within loop, false if loop hasn't started or ended.

Usage

if( in_the_loop() ){
	// we inside the loop...
}

Examples

#1 Change post title

The below example shows how to add a hook that changes the title when the_title() function is used within The Loop.

add_filter( 'the_title', 'modify_single_post_entry_titles' );

function modify_single_post_entry_titles( $title ) {

	if ( is_singular( 'post' ) && in_the_loop() ) {
		// Change title
		$title = 'A new title.';
	}

	return $title;
}

Notes

  • Global. WP_Query. $wp_query Global WP_Query instance.

Changelog

Since 2.0.0 Introduced.

Code of in the loop: wp-includes/query.php WP 5.2.2

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

Related Functions

From tag: Conditional tags (all)

More from category: Queries

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

vladlu 100vlad.lu
Editors: kama 100
No comments
    Hello, !     Log In . Register