2

I am using the Multipostthumbnails plugin - https://wordpress.org/plugins/multiple-post-thumbnails/

And it has a certain naming convention to it. I know the usual way to output a placeholder if no featured image set, but not with their code?

I obviously want something like -

if ( has_post_thumbnail($post->ID) ){   
$image = wp_get_attachment_image_src(get_post_thumbnail_id(), 'news-large');
$image = $image[0];
echo '<img src="'.$image.'" alt="" />';
} else {  
$image = get_template_directory_uri() .'/assets/img/placeholders/placeholder.png'; 
echo '<img src="'.$image.'" alt="" />';
}

But their existing code to show a featured image is -

if (class_exists( 'MultiPostThumbnails' )) : MultiPostThumbnails::the_post_thumbnail(get_post_type(), 'header-image', NULL, 'header-image-full', array('class' => "custom-header-image") ); endif;

Just cannot see how to adjust this to fit my code at the top?

For Blog Pages -

Standard Code -

if (class_exists( 'MultiPostThumbnails' )) : MultiPostThumbnails::the_post_thumbnail( 'page', 'header-image', get_option('page_for_posts'), 'header-image-full', array('class' => "custom-header-image") ); endif;

My attempt to try and show placeholder -

if ( class_exists( 'MultiPostThumbnails' ) ) { 
                        MultiPostThumbnails::the_post_thumbnail( 'page', 'header-image',     get_option('page_for_posts'),
                            'header-image-full', 
                            array('class' => "custom-header-image") ); 

                        } else {  
                            $image = get_template_directory_uri() .'/assets/img/placeholders/header_placeholder.png'; 
                            echo '<img src="'.$image.'" alt="" />';
                        }

Many thanks

2 Answers 2

2

I think that you're adding the code inside the loop of index.php or blog template file. You might be try this kind of things:

if ( class_exists( 'MultiPostThumbnails' ) && 
     ( MultiPostThumbnails::has_post_thumbnail( get_post_type(), 'header-image' , get_the_ID() ) ) ) { 
       MultiPostThumbnails::the_post_thumbnail(get_post_type(), 'header-image', NULL, 
                          'header-image-full', 
                           array('class' => "custom-header-image") ); 

} else {  
  $image = get_template_directory_uri() .'/assets/img/placeholders/placeholder.png'; 
  echo '<img src="'.$image.'" alt="" />';
}
8
  • Hi Chinmoy, I have it inside header.php. Tried your code, but it just returns the placeholder image, even though the custom image is set? thanks Aug 8, 2014 at 11:32
  • Are you add the code inside the while loop? Please change this line if ( has_post_thumbnail($post->ID) ){ with if ( has_post_thumbnail(get_the_ID() ) ){ Aug 8, 2014 at 11:36
  • It is outside of the loop, I have added new code to my original post so you can see the exact code I am using (including yours) but still only showing placeholder? Aug 8, 2014 at 11:41
  • Ok. I just checked the plugin's code. has_post_thumbnail() is taking three parameters. But we are passing one parameter, for this reason if statement is not working. I edited the code again. Please see the code. Aug 8, 2014 at 11:47
  • Tried the latest code. Still just returns the placeholder? Aug 8, 2014 at 11:55
1

Next time having a look at the docs might help ;)

MultiPostThumbnails::has_post_thumbnail(
    $post_type,
    $id,
    $post_id = null
)

Source: https://github.com/voceconnect/multi-post-thumbnails/wiki/Functions-and-Template-Tags#has_post_thumbnail

I guess you can make it from here. If not let us know.

1
  • Thanks kraftner ;) Just having trouble putting all my existing data into the function they mention? Aug 8, 2014 at 10:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.