How to stop featured image thumbnail [set as background image] on blog index page just repeating same image across all posts

Question

How to stop featured image thumbnail [set as background image] on blog index page just repeating same image across all posts:

i have this code here:

add_action( 'wp_head', 'set_featured_background', 99);
function set_featured_background() {
    if ( has_post_thumbnail() ) { 
        $featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'full');
        ?>
        <style>
        .has-post-thumbnail {
            background-image: url('<?php echo $featured_img_url ?>');
            background-size: 100% 100%; 
        }
        </style>
        <?php
    }
}

And ive tried a few different variations but it always jsut finds the most recent posts image and uses that across all posts. Do i need to be trying to put in a check to make sure it does the same thing every unique post. For example: If this post has a unique ID find its thumbnail image and apply as a style. Or is it because im trying to add it to the css class has-post-thumbnail which is applied to all posts? [that have a thumbnail]

0
Moose 2 months 0 Answers 7 views 0

Leave an answer