The generate_header_element_display
allows us to bypass the Display Rules, so we can enable or disable an Element under our own conditions.
Examples
For example, if we want to assign a specific header to Author Tom only:
add_filter( 'generate_header_element_display', function( $display, $element_id ) {
if ( 10 === $element_id && is_author( 'Tom' ) ) {
$display = true;
}
return $display;
}, 10, 2 );
Another request that we get from time to time is to only show page hero on first archive or posts page and turn off for 2nd and subsequent pages of posts. This can be done with the snippet below:
add_filter( 'generate_header_element_display', function( $display ) {
if ( is_paged() ) {
$display = false;
}
return $display;
} );
Or we can disable the header element if no featured image is added:
add_filter( 'generate_header_element_display', function( $display, $element_id ) {
if ( 123 === $element_id ) { // Only target specific Element
if ( ! has_post_thumbnail() ) {
$display = false;
}
}
return $display;
}, 10, 2 );
Similarly, we can disable the header element if no featured image is added or if featured image is disabled using the disable element metabox:
add_filter( 'generate_header_element_display', function( $display, $element_id ) {
if ( 123 === $element_id ) { // Only target specific Element
if ( !has_post_thumbnail($post->ID) || get_post_meta( get_the_ID(), '_generate-disable-post-image', true ) === 'true' ) {
$display = false;
}
}
return $display;
}, 10, 2 );