post thumbnails - featured image background

trying to create a shortcode in funtions.php to show a featured post.. Everything I have works fine except I can't

trying to create a shortcode in funtions.php to show a featured post..

Everything I have works fine except I can't get the background-image to show the post's featured image...

echo $featured_post_url displays the correct address, but I just can't get it to show as the div's background image...

maybe ive been staring at the computer screen too long... can anybody tell me what i'm missing here?

    function propertunity_featured_post() {
      $featured_post_id = 2860;
      $featured_post = get_post($featured_post_id);
      $featured_post_url = get_permalink($featured_post_id);

      $featured_post_image_url = wp_get_attachment_url( 
      get_post_thumbnail_id($featured_post_id, 'thumbnail') );

      echo "

        <div class='card'>

          <div class='card-header' style='background-color: #f8f8fc; overflow: hidden;'>

            <a href='" . $featured_post_url . "'>

              <div class='card__image' style='background-image: url(' " . $featured_post_image_url . " ');'></div>       

            </a>

          </div>

       </div>";

trying to create a shortcode in funtions.php to show a featured post..

Everything I have works fine except I can't get the background-image to show the post's featured image...

echo $featured_post_url displays the correct address, but I just can't get it to show as the div's background image...

maybe ive been staring at the computer screen too long... can anybody tell me what i'm missing here?

    function propertunity_featured_post() {
      $featured_post_id = 2860;
      $featured_post = get_post($featured_post_id);
      $featured_post_url = get_permalink($featured_post_id);

      $featured_post_image_url = wp_get_attachment_url( 
      get_post_thumbnail_id($featured_post_id, 'thumbnail') );

      echo "

        <div class='card'>

          <div class='card-header' style='background-color: #f8f8fc; overflow: hidden;'>

            <a href='" . $featured_post_url . "'>

              <div class='card__image' style='background-image: url(' " . $featured_post_image_url . " ');'></div>       

            </a>

          </div>

       </div>";
Share Improve this question asked Aug 20, 2019 at 21:05 Liquid_Shane_OLiquid_Shane_O 391 silver badge7 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You can try wp_get_attachment_image_src this for getting Feature Image URL. Here is the Full code.

<?php
function propertunity_featured_post() {
      $featured_post_id = 2860;
      $featured_post = get_post($featured_post_id);
      $featured_post_url = get_permalink($featured_post_id);
      $featured_post_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($featured_post_id, 'thumbnail') );
      echo "<div class='card'>
          <div class='card-header' style='background-color: #f8f8fc; overflow: hidden;'>
            <a href='" . $featured_post_url . "'>
              <div class='card__image' style='background-image: url(' " . $featured_post_image_url[0] . " ');'></div>       
            </a>
          </div>
       </div>";
}
?>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745229891a4617628.html

相关推荐

  • post thumbnails - featured image background

    trying to create a shortcode in funtions.php to show a featured post.. Everything I have works fine except I can't

    4小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信