A user asked:
I am trying to edit Business Pro so the individual blog posts use the default header image instead of pulling the featured image into the header. Is this possible or something you’ve already covered? I’m stuck.
On single post pages in Business Pro, featured image will appear as the page header below the site header. The default header image (can be changed in the Customizer) will appear when there is no featured image.
If you would like to display the default header image regardless of whether a featured image is present or not, simply edit includes/helpers.php and change
if ( ! $url ) {
to
if ( ! $url || is_singular( 'post' ) ) {
inside the business_custom_header()
function definition.
Sample Screenshots:
Appearance > Header Media:
Before (a post with featured image):
After:
Hi Sridhar, I updated the code but it is still pulling in the featured image as the hero image when I add a featured image to CPT pages, Pages, Posts. Is there something else I need to do?
Thank you!!
Here is the code:
/**
* Custom header image callback.
*
* Loads custom header or featured image depending on what is set on a per
* page basis. If a featured image is set for a page, it will override
* the default header image. It also gets the image for custom post
* types by looking for a page with the same slug as the CPT e.g
* the Portfolio CPT archive will pull the featured image from
* a page with the slug of ‘portfolio’, if the page exists.
*
* @since 1.0.5
*
*
* @return string
*/
function business_custom_header() {
$id = ”;
// Get the current page ID.
if ( class_exists( 'WooCommerce' ) && is_shop() ) {
$id = wc_get_page_id( 'shop' );
} elseif ( is_post_type_archive() ) {
$id = get_page_by_path( get_query_var( 'post_type' ) );
$id = $id->ID && has_post_thumbnail( $id->ID ) ? $id->ID : false;
} elseif ( is_category() ) {
$id = get_page_by_title( 'category-' . get_query_var( 'category_name' ), OBJECT, 'attachment' );
} elseif ( is_tag() ) {
$id = get_page_by_title( 'tag-' . get_query_var( 'tag' ), OBJECT, 'attachment' );
} elseif ( is_tax() ) {
$id = get_page_by_title( 'term-' . get_query_var( 'term' ), OBJECT, 'attachment' );
} elseif ( is_front_page() ) {
$id = get_option( 'page_on_front' );
} elseif ( 'posts' === get_option( 'show_on_front' ) && is_home() ) {
$id = get_option( 'page_for_posts' );
} elseif ( is_search() ) {
$id = get_page_by_path( 'search' );
} elseif ( is_404() ) {
$id = get_page_by_path( 'error' );
} elseif ( is_singular() ) {
$id = get_the_id();
}
if ( is_object( $id ) ) {
$url = wp_get_attachment_image_url( $id->ID, 'hero' );
} elseif ( $id ) {
$url = get_the_post_thumbnail_url( $id, 'hero' );
}
if ( ! $url || is_singular( 'post' ) ) {
$url = get_header_image();
}
if ( $url ) {
$selector = get_theme_support( 'custom-header', 'header-selector' );
return printf( '<style id="hero-section" type="text/css">' . esc_attr( $selector ) . '{background-image:url(%s)}</style>' . "n", esc_url( $url ) );
} else {
return '';
}
}
This is perfect fixed my issue, is there a way to also do it for category pages?