Site logo

Archived topic

updating the header in the posts page

7 replies · Started by eric on October 2, 2019

Viewing posts 1–8 of 8

Hi there,

I've updated the header.php file to create my own banner throughout the site I'm working on. It works as intended on all pages except one.

One the posts page (assigned on the settings, reading page), it pulls up the image and title of the first POST, instead of the pages title and featured image. Is there an easy way to change this? Thanks in advance.

global $post;
if (!is_page_template( 'SDhome.php' ) ) {
if ( has_post_thumbnail() ) {
echo '

';
$featured_image = get_the_post_thumbnail_url($post, 'hero-image' );
$title = get_the_title();
echo '

';
echo $title;
echo '

';
echo '<div class = "hero" style= " background-image: url( ';
echo $featured_image;
echo ')">';
echo '

';
echo '

';
echo '

';
}
} else { // home page

};

I like this approach but here are the issues I'm experiencing with it.

1) is it possible to control the size of the featured image that comes up. Ideally, I would like to use a customized size.

2) This functionality doesn't address my initial problem of the blog posts featured image. It pulls up the "fall back" image but not the one specified on the page. Is that possible to change?

3) Is there an easy way to change the title from "blog" to something else?

Hi there,

1. the Header Element sets the featured Image as a CSS Background Image. So to change its size you need to adjust the top / bottom padding to increase the elements height.

2 and 3. The Blog page is not a standard page for one it doesn't use the_title function and WP ignores meta boxes including featured image. So for the blog you would need to create a separate header element and set the title and background image you need.

Thanks for the clarification. On my first question, I meant the file size, not the image size. I have a number of customized media sizes (e.g. hero, thumbnail, etc.) and it would be helpful to pull up the appropriate size. The header function seems to only pull up up the image's original size? Thank you in advance.

Hi there,

You could try something like this:

add_filter( 'generate_page_hero_background_image_url', function( $image_url, $options ) {
    if ( is_singular() && has_post_thumbnail() ) {
        $image_url = get_the_post_thumbnail_url( get_the_ID(), 'large' );
    }

    return $image_url;
}, 10, 2 );

Just change full to whatever size you want to use :)

That works! Thanks very much! I really appreciate it!

You're welcome :)

This archived topic is closed to new replies.