$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>post thumbnails - How to convert feature image as background image|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

post thumbnails - How to convert feature image as background image

matteradmin7PV0评论

I am trying to set background image out of feature image so I used the background-size:cover. Here is the code I have done but how can I add feature image into <div class="bg-img" style="background:...">. How can I achieve background image technique.

if ( $post->post_parent )
    $post_id = $post->post_parent;
else
    $post_id = $post->ID;

if ( $thumbnail = get_the_post_thumbnail( $post_id, 'post-thumbnail' ) ) {
    echo $thumbnail;
}

I am trying to set background image out of feature image so I used the background-size:cover. Here is the code I have done but how can I add feature image into <div class="bg-img" style="background:...">. How can I achieve background image technique.

if ( $post->post_parent )
    $post_id = $post->post_parent;
else
    $post_id = $post->ID;

if ( $thumbnail = get_the_post_thumbnail( $post_id, 'post-thumbnail' ) ) {
    echo $thumbnail;
}
Share Improve this question edited Nov 15, 2018 at 13:08 fuxia 107k39 gold badges255 silver badges461 bronze badges asked Nov 15, 2018 at 6:20 William JeromeWilliam Jerome 1836 silver badges13 bronze badges 1
  • 1 You can use get_the_post_thumbnail_url() for get image url and put into <div class="bg-img" style="background:PHP VARIABLE HERE"> – Pratik Patel Commented Nov 15, 2018 at 6:24
Add a comment  | 

1 Answer 1

Reset to default 0

please use this code

<?php $backgroundImg = wp_get_attachment_image_src( 
   get_post_thumbnail_id($post->ID), 'full' );?>

 <div class="header-wrap" style="background: url('<?php echo $backgroundImg[0]; ?>') no-repeat;">

 <header class="entry-header">

 <h1 class="entry-title">
 <?php the_title(); ?>
 </h1>

 </header>

Post a comment

comment list (0)

  1. No comments so far