Skip Navigation

[Resolved] Featured image as background

This support ticket is created 5 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 3 replies, has 2 voices.

Last updated by Shane 5 years, 7 months ago.

Assisted by: Shane.

Author
Posts
#1227202

How would the code from this post https://toolset.com/forums/topic/how-to-add-a-custom-posts-featured-image-as-a-background-for-a-div-in-views/ look if I want a larger image to use as background for a div? (not the thumbnail as in this code)

I am looking to use this for showing blog posts in sidebars, and also want to use it to output text on top of the blog post images in post archives.

By the way I am using Visual Composer, and I tried using their grid builder, but I was not able to output the custom field data on top of the images, and so I decided to get more freedom by building a custom view.

Thanks, regards
Lykke

#1227397

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Lykke,

Thank you for contacting our support forum.

What when you are using the code below.


// Add Shortcode
function get_featured_url($atts) {
// Attributes
  $vals=shortcode_atts(
    array(
      'post_id' => '',
    ), $atts );
  
$feat_image = wp_get_attachment_url( get_post_thumbnail_id($vals["post_id"]) );
return "style = 'background-image: url(". esc_url($feat_image).");height: 120px; width: 120px;'";
}
  
add_shortcode( 'get_featured_url', 'get_featured_url' );

You can remove the height: 120px; width: 120px; configuration from the shortcode and it should display the image in its entirety.

Thanks,
Shane

#1228240
htmloutput.PNG

Hi Shane,

Thanks for your reply. I have added the code to my functions file, but having trouble with the output. Since I'm trying to output featured image from blog posts and not an "album" post type, I wrote "post" instead. But it's not working.

<div class="cover-container" [get_featured_url post_id="[wpv-post-id id='$post']"]>

#1228645

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Lykke,

Its not rendering the code correctly it seems.

Would you mind allowing me to have admin access to the page so that I can have a look at this for you ?

Thanks,
Shane