WordPress.org

Ready to get started?Download WordPress

Forums

If the_post_thumbnail else not working (10 posts)

  1. trishahdee
    Member
    Posted 1 year ago #

    "specials" and "featured-slider" are defined in functions.php as new_image_size. In a template I want "featured-slider" to show only if there is no "specials" thumbnail.

    The following code is showing both and I can't figure out why...

    <?php if ( the_post_thumbnail('specials') )  {
      the_post_thumbnail('specials');
    }
    else {
    	the_post_thumbnail('featured-slider');
    }
    ?>
  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    you could try using get_the_post_thumbnail($post->ID,'specials')

    http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail

    however, I am not quite sure what you are trying to check with that conditional statement?

  3. trishahdee
    Member
    Posted 1 year ago #

    Thanks for the reply alchymyth. Yes that will also get the "specials" thumbnail.

    The problem I'm having is that my code works, but BOTH of the images are showing. Both the "specials" and the "featured-slider" are showing at the same time. I think there is something wrong with my else code but I think my brain is just too tired to see it.

  4. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    I repeat my question:

    what you are trying to check with that conditional statement?

    if you are trying to check the width or size of the originally uploaded featured image, then your code will not work;

    look into header.php of Twenty Eleven for inspiration for how to check the width of a featured image.

  5. trishahdee
    Member
    Posted 1 year ago #

    I'm sorry, I thought I was being clear. Let me try again... What I want is this:

    If the post has a "special" thumbnail then echo that.

    If there is no "special" thumbnail, then echo the "featured-slider".

    I'm only checking if the "special" thumbnail exists.

  6. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    If the post has a "special" thumbnail

    there is no meaning to 'special' apart form giving the thumbnail a size for the output.

    you can only check if a thumbnail exists for a post, and you can check the size of the original full sized image.

    'special' and 'featured-image' etc refer to sizes for the output - http://codex.wordpress.org/Post_Thumbnails

  7. trishahdee
    Member
    Posted 1 year ago #

    And yet, the first part of the code is working to show the "special" image. The "special" post type is for special offers or coupons posts. It has a different size than the default or the featured-slider images.

    My code is showing BOTH. I only want to show the "special" one if it exists.

    Blog Post Example

  8. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    And yet, the first part of the code is working to show the "special" image.

    no - it is not.

    this wrong impression comes from the fact the actually the the_post_thumbnail('specials') from with in the if ( ... ) gets output, not the one in the first set of { ... }, and then the the the_post_thumbnail('featured-slider') in the second set of { ... }.

    The "special" post type is for special offers or coupons posts.

    the 'special' in the 'the_post_thumbnail()' code has nothing to do with post types - it is purely a size indicator.

    to check for post types, you could try (make sure if it is 'special' or 'specials'):

    <?php if ( get_post_type() == 'special' )  {
      the_post_thumbnail('specials');
    }
    else {
    	the_post_thumbnail('featured-slider');
    }
    ?>

    http://codex.wordpress.org/Conditional_Tags#A_Post_Type

  9. trishahdee
    Member
    Posted 1 year ago #

    Oh, I see where you are going with that. And I did check the correct spelling of the thumbnail, but it's not finding the "special" one:

    Blog Post Expample #2

  10. trishahdee
    Member
    Posted 1 year ago #

    I got it... This works:

    <?php if ( has_post_thumbnail() == 'specials' )  {
      the_post_thumbnail('specials');
    }
    else {
    	the_post_thumbnail('featured-slider');
    }
    ?>

    Blog Post Example #3

    Thank you for your help. I don't think I would have gotten the answer without your guidance :D

Topic Closed

This topic has been closed to new replies.

About this Topic