WordPress at Your Fingertips

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 WordPress Query object.

Changelog

Since 2.0.0 Introduced.

Code of in_the_loop() WP 5.8.1

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

Conditional tags (all)

Queries

Posts, Pages, ...

vladlu 100vlad.lu
Editors: Kama 100
No comments
    Log In