voceconnect / multi-post-thumbnails

Adds multiple post thumbnails to a post type. If you've ever wanted more than one Featured Image on a post, this plugin is for you.
143 stars 63 forks source link

Using as background image on a page #69

Closed ghost closed 9 years ago

ghost commented 9 years ago

Hello, I would like to use this to display a background image on a page, not a post.

Heres the code i am using: style="background: url(<?php if (class_exists('MultiPostThumbnails')) : MultiPostThumbnails::get_post_thumbnail_id('page', 'background-image'); endif; My function: if (class_exists('MultiPostThumbnails')) { new MultiPostThumbnails(array( 'label' => 'Background Image', 'id' => 'background-image', 'post_type' => 'page' ) ); }

However it does not display. When I use the regular "the_post_thumbnail" in the theme code, it will work correctly, but thats to generate the full image tag. Please let me know how to accomplish this on a page. Thanks.

chrisscott commented 9 years ago

You'd need to use MultiPostThumbnails ::get_post_thumbnail_url() not MultiPostThumbnails::get_post_thumbnail_id() to get the URL of the thumbnail.

ghost commented 9 years ago

Still does not pull the image: `

` Generates: `
`
matgargano commented 9 years ago

MultiPostThumbnails::get_post_thumbnail_url() looks like it returns a string, you have to echo it...

so change

<section id="hero" class="major col-md-12" style="background: url(<?php if(class_exists('MultiPostThumbnails')) : MultiPostThumbnails::get_post_thumbnail_url('page','background-image'); endif; ?>)">

to:

<section id="hero" class="major col-md-12" style="background: url(<?php

if (class_exists('MultiPostThumbnails')) : esc_attr_e( MultiPostThumbnails::get_post_thumbnail_url('page','background-image') ); endif; ?>)">

I wrapped it in esc_attr_e for proper escpaping...

I didn't test this, but it should work... my formatting skills are not up to task, apologies.

ghost commented 9 years ago

This fixed the issue. Thank you for your help!