WordPress.org

Ready to get started?Download WordPress

Forums

Multiple Post Thumbnails
[resolved] Conditional Default Image (10 posts)

  1. dadvan
    Member
    Posted 1 year ago #

    I am trying to do something like this but my syntax is all wrong please assist:

    <?php if (class_exists('MultiPostThumbnails')) : MultiPostThumbnails::the_post_thumbnail(get_post_type(), 'secondary-image'); } else { ?>
    								<img src="/images/default.png" alt="<?php the_title(); ?>" />
    								<?php } ?>

    http://wordpress.org/extend/plugins/multiple-post-thumbnails/

  2. dadvan
    Member
    Posted 1 year ago #

    Anybody???

  3. dadvan
    Member
    Posted 1 year ago #

    Anybody?

  4. barryp
    Member
    Posted 1 year ago #

    Let me know if this doesn't work for you.

    // Check for Plugin
    if (class_exists('MultiPostThumbnails')) {
    
    	// Set Thumbnail
    	$thumb = MultiPostThumbnails::the_post_thumbnail(get_post_type(), 'preview-image');
    
    	// Thumbnail exist? Else show Not Found
    	if ($thumb) : echo $thumb; else : echo '<img src="url/to/default/image.jpg">'; endif;
    
    	// Plugin not found.
    	} else {
    
    		echo "MultiPostThumbnails Not Found.";
    }
  5. dadvan
    Member
    Posted 1 year ago #

    Thanks Barryp! Something is not quite right here it is putting the fallback image on top of the Multipost Thumbnail:

    <?php // Check for Plugin
                  if (class_exists('MultiPostThumbnails')) {
    
                    // Set Thumbnail
                    $thumb = MultiPostThumbnails::the_post_thumbnail(get_post_type(), 'secondary-image');
    
                    // Thumbnail exist? Else show Not Found
                    if ($thumb) : echo $thumb; else :?><img src="/images/icon.png" alt="<?php the_title(); ?>" /><?php ; endif;
    
                    // Plugin not found.
                  } else { ?>
    <img src="/images/icon.png" alt="<?php the_title(); ?>" />
                 <? }?>
  6. barryp
    Member
    Posted 1 year ago #

    Just noticed this. Give me a few.

  7. barryp
    Member
    Posted 1 year ago #

    Sorry for the delay. After looking through the plugin code, there is a has_post_thumbnail option. I came up with this that I'm sure could be written better, I'm still learning.

    // Check for Plugin
                  if (class_exists('MultiPostThumbnails')) {
    
                    // Set Thumbnail
                    $thumb = MultiPostThumbnails::the_post_thumbnail(get_post_type(), 'preview-image');
                    $has_thumb = MultiPostThumbnails::has_post_thumbnail(get_post_type(), 'preview-image', strval(get_the_ID()));
    
                    // Thumbnail exist? Else show Not Found
                    if ($has_thumb) : echo $thumb; else : echo '<img src="'. get_bloginfo('template_directory') . '/img/default_thumb.jpg" />'; endif;
    
                    // Plugin not found.
                  } else {
    
                    echo "MultiPostThumbnails Not Found.";
    
                  }
  8. oysterdesign
    Member
    Posted 1 year ago #

    Barryp. This works a treat.

    Thanks!

  9. dadvan
    Member
    Posted 1 year ago #

    Works, Thanks barryp!

  10. barryp
    Member
    Posted 1 year ago #

    Sweet!! Glad it works for ya.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic