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

Reading the secondary-image. #71

Closed NielsTieman closed 9 years ago

NielsTieman commented 9 years ago

Hi,

First of all; thanks for the plugin!

I got everything working with one featured image. Now I added the the plugin for a second one and then the problems start. But I can't seem to figure out how to change my code to look at the secondary-image.

I can't seem to figure out what to change in my code to get this working, and it has been hours. So, thanks in advance for the tips and hopefully a solution.

<?php $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnails' );?><div class="background" style="background-image: url('<?php echo $src['0'];?>')"></div>
kevinlangleyjr commented 9 years ago

If you just use get_post_thumbnail_id() you'll only be getting the default thumbnail.

To get the secondary, you should be using https://github.com/voceconnect/multi-post-thumbnails/wiki/Functions-and-Template-Tags#get_post_thumbnail_url and the id parameter should be whatever you named the secondary image. So if you just used the example code to register that image like here, https://github.com/voceconnect/multi-post-thumbnails/wiki#register-a-new-thumbnail, then it would be secondary-image.

<?php 
$src = MultiPostThumbnails::get_post_thumbnail_url( get_post_type( $post->ID ), 'secondary-image', $post->ID, 'thumbnails' );
?>
<div class="background" style="background-image: url('<?php echo $src; ?>')"></div>

But you should also add a check in there to ensure that $src is not false before trying to set it as the background image as well as run esc_url() on the value.

NielsTieman commented 9 years ago

Kevin,

Thank you so much. This works like a charm. I wil look into the $src and the esc_url().