Skip to content

Instantly share code, notes, and snippets.

@tomjn
Last active December 11, 2015 05:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomjn/4551786 to your computer and use it in GitHub Desktop.
Save tomjn/4551786 to your computer and use it in GitHub Desktop.
hmmm initialise in the loop start, loop_end shouldn't ever be called without loop_start
<?php
add_action( 'loop_start', 'qd_loop_start' );
add_action( 'loop_end', 'qd_loop_end' );
function qd_loop_start( $query ) {
global $query_depth;
if(!isset($query_depth)){
$query_depth = 0;
}
$query_depth++;
}
function qd_loop_end( $query ) {
global $query_depth;
$query_depth--;
}
function get_query_depth(){
global $query_depth;
if(!isset($query_depth)){
$query_depth = 0;
}
return $query_depth;
}
function my_in_main_loop() {
$result = ( get_query_depth() == 1) && ( is_main_query() ) );
return $result;
}
@tomjn
Copy link
Author

tomjn commented Jan 16, 2013

note to self, needs another bit of code to initialize $query_depth other than get_query_depth itself

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment