Support » Developing with WordPress » Featured post image in CSS background image?

  • I am trying to develop an amazing theme and the theme has it’s background image in CSS not in the Html code, I would like to know if and how could I get a featured post image to become available in the CSS code. The theme will be converted from The Big Picture bootstrap theme and it’s code is a little weird for me. I mean, in it’s CSS file is present a .full class but not present in Html anywhere.

    If some of you fine coders would like to help me get this to work, I would very much appreciate it.

    P.S. This theme will power my website, it is not meant to be released…but who knows…

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator Steve Stern

    (@sterndata)

    Support Volunteer

    You could take the featured image, get the URL, and add some in-line CSS to set the background image.

    https://developer.wordpress.org/reference/functions/get_the_post_thumbnail_url/

    I managed to find the .full class, it was on the <html> tag, weird right? Now I’ve moved it to the <body> tag, but still don’t know if this is possible to do.
    Is there a way that the featured image could live outside of a post? Like so in the <body> tag?

    Moderator bcworkz

    (@bcworkz)

    In order to get the featured image, you need the current post ID. Except for single post requests, this is only available inside the loop. If you only need this for single post views, the post ID can be obtained outside the loop with get_queried_object_id(). Then, like Steve suggested, you can output background CSS inside of <style> tags anywhere that’s appropriate.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Featured post image in CSS background image?’ is closed to new replies.