Wordpress - How to get thumbnail in an HTML tag with CSS and PHP?

Asked

Viewed 103 times

0

I have a CSS that is like this:

.cid-rlWhG48NmK {
    padding-top: 60px;
    padding-bottom: 0px;
    background-image: url("../../images/background4.jpg");
}

And my HTML calls it this way:

<section class="mbr-section content5 cid-rlWhG48NmK mbr-parallax-background" id="content5-f">
...

I want to implement the get_the_post_thumbnail() in the cid-rlWhG48NmK changing the image: If you have a highlighted image, then use a pattern otherwise.

I’m trying this way:

    <style>
    <?php if(get_the_post_thumbnail()) : ?>
        .cid-rlWhG48NmK {
            background-image: url('<?php the_post_thumbnail(); ?>')>;
        }
    <?php else : ?> 
        .cid-rlWhG48NmK {
            background-image: url('<?php bloginfo($show = ´template_url´); ?>/assets/images/background4.jpg');
        }
    <?php endif; ?>
</style>

<section class="mbr-section content5 cid-rlWhG48NmK mbr-parallax-background" id="content5-f">

But it’s not taking either the highlighted image or the defined image otherwise.

Does anyone know how to solve?

PS: I remove the background-image from CSS to make the code I want.

1 answer

2


To get the thumbnail url just use the function: get_the_post_thumbnail_url() recommend putting it inline in html, example:

if ( has_post_thumbnail() ) {
    $img = get_the_post_thumbnail_url();
}
else {
    $img = 'caminh_imagem_padrao'.png
}
?>

    <section class="mbr-section content5" id="content5-f" style="background-image: url(<?php echo $img ?>);">
  • 1

    Glaydson Rodrigues, it worked out! Thank you very much! :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.