Show Blog Page Title On 1st Page Only

This code removes the title from the Genesis blog page template on all paginated pages except the first page.

Based on this question :

Is there a way to only show the blog archive title on the first page and not on subsequent ones.

add_action( 'genesis_before_loop', 'remove_blog_page_title_conditionally', 1 );
function remove_blog_page_title_conditionally() {

if ( ! get_query_var( 'paged' ) >= 2 ) 
	 return;
		
remove_action( 'genesis_before_loop', 'genesis_do_blog_template_heading' );

}

Add to your child themes functions file.

You could also write the code like this :

add_action( 'genesis_before_loop', 'remove_archive_headline', 9 );
function remove_archive_headline() {

if ( get_query_var( 'paged' ) >= 2 ) {
		
remove_action( 'genesis_before_loop', 'genesis_do_blog_template_heading' );
   }
   
}

How The Code Works

Uses get_query_var to get the paged key which stores the pagination number 2 you can see in the following permalink example.

/blog/page/2/

>= is a comparison operator in PHP which returns true if the page number is 2 or greater. If the paged value is 2 or greater, the remove_action code is executed removing the blog page template heading on the 2nd and subsequent pages.

The entire code needs to run before the blog page heading is executed so the hook used, genesis_before_loop, needs to fire before the default hook, genesis_before_loop, which adds the blog page title.

In this case we use the same hook in the add_action function, genesis_before_loop, however, we add a 3rd parameter 1 which executes before the default hook priority which is 10. If you used a 3rd parameter of 11 or higher, the code would not work as it would fire too late.

You could use any hook which fires before genesis_before_loop.

Test and you will see.

Related Code Snippets

Join 5000+ Followers

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