Genesis Blog Page Template Conditional Within The Loop

If you want to target the blog page template in Genesis, within the loop, you have at least 3 options:

  1. You can create a custom blog page template and add code directly to the file which includes any hook that fires within the loop. This will work.
  2. Use query_args as a custom field to customize what posts are shown using your blog template
  3. Use a conditional tag that works

The only problem is, the is_page_template() conditional tag is not available within the loop or any loop that modifies $wp_query because it changes all the conditionals therein.

This basically rules out using is_page_template( 'page_blog.php' ) in your functions file to target any page in Genesis which uses the blog page template when used with any hook that executes within the loop. Functions which include hooks that fire outside the loop will work.

Doesn’t Work

Example: Paste the following code in your child themes functions file which won’t work.

add_action( 'loop_start', 'fails' );
function fails() {
if ( is_page_template( 'page_blog.php' ) )
echo( 'Doesn\'t Work Within The Loop' );
}

Any function using the loop_start hook or any other WordPress or Genesis hook which executes within the loop will fail when using the is_page_template( 'page_blog.php' ) conditional tag in Genesis.

Adding the above code to a custom blog page template file, will work.

Note: The above code WILL work if you change the hook from loop_start to any WordPress or Genesis hook which fires outside the loop like genesis_before_loop or wp_head.

The Solution

The following code for members enables you to execute code within the loop on any page in Genesis which loads the blog page template:

Line 220 of genesis > lib > functions > general.php contains the following function:

function genesis_is_blog_template() {

global $wp_the_query;

return 'page_blog.php' === get_post_meta( $wp_the_query->get_queried_object_id(), '_wp_page_template', true );

}

Works

You can now use genesis_is_blog_template() to check if WordPress is loading the blog page template. Example:

add_action( 'loop_start', 'works' );
function works() {
if ( genesis_is_blog_template() )
echo( 'Works Within The Blog Page Template Loop' );
}

Related Tutorials

Join 5000+ Followers

Get The Latest Free & Premium Tutorials Delivered The Second They’re Published.