Site logo

Archived topic

Show featured images as inside-article background on blog archives

6 replies · Started by Andrew on January 22, 2019

Viewing posts 1–7 of 7

Hi there,

You'd likely need to create a custom page template in order to do that, unfortunately. Without doing that, you'd need to re-query the posts in wp_head and loop through them to create CSS for each individual post.

From a simplicity/performance standpoint, the custom template is the way to go.

For example, copy content.php and add it to your child theme.

Then, right above <article>, do this:

<?php
$image = get_the_post_thumbnail_url( get_the_ID() );
$background_css = '';

if ( $image ) {
    $background_css = 'style="background-image: url( ' . $image . ' );"';
}
?>

Then you'd add $background_css to the <article> element.

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?> <?php generate_do_microdata( 'article' ); ?> <?php echo $background_css; ?>>

Great, I'll give that a go. But thinking about it, wouldn't this be a better thing to do with your WP Show Posts plugin?

Try applying the image to the .inside-article element instead.

Great, yes, of course, thanks!

You're welcome :)

This archived topic is closed to new replies.