in_the_loop()
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. |
in_the_loop() in the loop code WP 6.7.1
function in_the_loop() { global $wp_query; if ( ! isset( $wp_query ) ) { return false; } return $wp_query->in_the_loop; }