[Resolved] Show featured images as inside-article background on blog archives

Home Forums Support [Resolved] Show featured images as inside-article background on blog archives

Home Forums Support Show featured images as inside-article background on blog archives

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #789082
    Andrew

    https://generatepress.com/forums/topic/making-blog-inside-article-div-show-featured-image-as-a-background-image/

    That query linked above has a solution from Tom that gives the same featured image for all the inside-article divs on the archive pages.

    How to fix it, so each archive entry shows its own featured image as a background?

    #789479
    Tom
    Lead Developer
    Lead Developer

    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; ?>>

    #789753
    Andrew

    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?

    #789777
    Andrew

    I tried your solution and I found the columns hard to control, specifically no margin between them and adding a margin messes up the layout.

    http://hotelutopia.pash.space/category/attractions/

    #790104
    Tom
    Lead Developer
    Lead Developer

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

    #790124
    Andrew

    Great, yes, of course, thanks!

    #790140
    Tom
    Lead Developer
    Lead Developer

    You’re welcome 🙂

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.