WordPress.org

Ready to get started?Download WordPress

Forums

Conditional Thumbnail Display? Check for thumbnail, if not, display code (20 posts)

  1. Ian
    Member
    Posted 3 years ago #

    So I've got a weird idea.

    I want wordpress to check to see if a post has a thumbnail / featured image. If it does, I want it to display the image, either in thumb size or one of the other featured image sizes depending on how I call it.

    but if there isn't one, I want wordpress to excecute different code. In this case I want it to use an isbn number gotten from a custom field to grab an image from amazon.

    That code I've got:

    <?php echo c2c_get_custom('isbn-10', '<img src="http://images.amazon.com/images/P/','.01.LZZZZZZZ.jpg" alt="Book Cover" /></a>'); ?>

    Though I'd like to replace "Book Cover" with the title, but wasn't sure if I could put php inside that other php. I'm still not clear on the formatting.

    Anyone know how I might do this?

  2. keesiemeijer
    moderator
    Posted 3 years ago #

    try this inside the loop:

    <?php
    if ( has_post_thumbnail()) {
    // do stuff if there is a post thumbnail
    } else {
    do stuff if there is not a post thumbnail
    }
    ?>

    I don't know how c2c_get_custom works but try something like this (in the loop):

    <?php
    $book_title = '.01.LZZZZZZZ.jpg" alt="Book Cover" />' . $post->post_title . '</a>';
    echo c2c_get_custom('isbn-10', '<img src="http://images.amazon.com/images/P/',$book_title); ?> ?>
  3. Ian
    Member
    Posted 3 years ago #

    wow, has_post_thumbnail, huh? That seems so simple. For some reason I assumed that something like that just didn't exist.

    I'll give it a shot and report back.

    about the c2c links, it's from a plugin that lets you use that function to get custom field info. I'm not sure if that's the standard way of doing it now but I've been doing that for a few wordpress versions now and it still works *shrug*

    I'm actually looking to replace the alt text "Book Cover" with the title of the post.

  4. keesiemeijer
    moderator
    Posted 3 years ago #

    Oops, try it with this:

    <?php
    $book_title = '.01.LZZZZZZZ.jpg" alt="'.$post->post_title.'" /></a>';
    echo c2c_get_custom('isbn-10', '<img src="http://images.amazon.com/images/P/',$book_title);
    ?>
  5. Ian
    Member
    Posted 3 years ago #

    I think that might work, I'll try it!

  6. Ian
    Member
    Posted 3 years ago #

    This is the basic code for my first part but I'm not sure how to do the echos properly. is there a good tutorial for php formatting for this kind of thing?

    <?php
    if (has_post_thumbnail()) {
    
    <a href="<?php $image_id = get_post_thumbnail_id();
    	$image_url = wp_get_attachment_image_src($image_id,'large', true);
    	echo $image_url[0];  ?>" class="thickbox">
    <?php the_post_thumbnail(); ?></a>
    
    } else {
        echo 'test';
    }
    
    ?>
  7. Ian
    Member
    Posted 3 years ago #

    ok, I'm rying it like this and it seems to be working?

    <?php
    if (has_post_thumbnail()) {
    
    $image_id = get_post_thumbnail_id();
    	$image_url = wp_get_attachment_image_src($image_id,'large', true);
    
    echo '<a href="';
    	echo $image_url[0];
    echo '" class="thickbox">';
    
    the_post_thumbnail();
    
    echo '</a>';
    
    } else {
        echo 'test';
    }
    
    ?>
  8. keesiemeijer
    moderator
    Posted 3 years ago #

    Yes this should work (I cleaned it up a little bit)

    <?php
    if (has_post_thumbnail()) {
      $image_url = wp_get_attachment_image_src(get_post_thumbnail_id(),'large', true);
      echo '<a href="' . $image_url[0] .'" title="' . the_title_attribute('echo=0') . '">';
      the_post_thumbnail();
      echo '</a>';
    } else {
        echo 'test';
    }
    ?>
  9. Ian
    Member
    Posted 3 years ago #

    thanks for all your help, it seems to be working really well:

    http://www.tradereadingorder.com/list/novels/fantasy/forgotten-realms/

    This is the code I'm currently using:

    <?php
    if (has_post_thumbnail()) {
      $image_url = wp_get_attachment_image_src(get_post_thumbnail_id(),'large', true);
      echo '<a href="' . $image_url[0] .'" title="' . the_title_attribute('echo=0') . ' class="thickbox" />
    ';
      the_post_thumbnail();
      echo '</a>';
    } else {
        $book_title = '.01.THUMBZZZ.jpg" alt="'.$post->post_title.'" />';
    echo c2c_get_custom('isbn-10-sc', '<a href="http://images.amazon.com/images/P/','.01.LZZZZZZZ.jpg" class="thickbox">');
    echo c2c_get_custom('isbn-10-sc', '<img src="http://images.amazon.com/images/P/',$book_title);
    }
    ?>

    I've got one final thing I'd like to try. That isbn-10-sc custom field has the isbn for the softcover book. But sometimes a release only has a hardcover. Is there a way to do an if statement with custom fields?

    I'd like to make it show a copy of that code with "hc" if the book has hardcover, a copy of the code with "sc" if the book has sc, or a copy of the code without either if it's a weird release (some just have isbn-10). I want it to cascade in that order. Any ideas?

    Again, thanks for all your help - you're saving me hours of trial and error!

  10. Ian
    Member
    Posted 3 years ago #

    huh, I messed something up with that last one and the thickbox wasn't working with the thumbnail image. This code works:

    <?php
    if (has_post_thumbnail()) {
      $image_url = wp_get_attachment_image_src(get_post_thumbnail_id(),'large', true);
    echo '<a href="' . $image_url[0] .'" class ="thickbox" title="' . the_title_attribute('echo=0') . '">';
      the_post_thumbnail();
      echo '</a>';
    } else {
        $book_title = '.01.THUMBZZZ.jpg" alt="'.$post->post_title.'" />';
    echo c2c_get_custom('isbn-10-sc', '<a href="http://images.amazon.com/images/P/','.01.LZZZZZZZ.jpg" class="thickbox">');
    echo c2c_get_custom('isbn-10-sc', '<img src="http://images.amazon.com/images/P/',$book_title);
    }
    ?>
  11. Ian
    Member
    Posted 3 years ago #

    Thought I might update you with the final code I ended up going with:

    <?php
    if (has_post_thumbnail()) {
      $image_url = wp_get_attachment_image_src(get_post_thumbnail_id(),'large', true);
    echo '<a href="' . $image_url[0] .'" class ="thickbox" title="' . the_title_attribute('echo=0') . '">';
      the_post_thumbnail();
      echo '</a>';
    }
    else {
    $isbn = c2c_get_custom('isbn-10-hc');
    list($Width) = @getimagesize("http://images.amazon.com/images/P/" . $isbn . ".01.THUMBZZZ.jpg");
    if ($Width > 1)
    {
    $book_title = '.01.THUMBZZZ.jpg" alt="'.$post->post_title.'" title="'.$post->post_title.'" />';
    echo c2c_get_custom('isbn-10-hc', '<a href="http://images.amazon.com/images/P/','.01.LZZZZZZZ.jpg" class="thickbox">');
    echo c2c_get_custom('isbn-10-hc', '<img src="http://images.amazon.com/images/P/',$book_title);
    }
    else {
    $isbn = c2c_get_custom('isbn-10-sc');
    list($Width) = @getimagesize("http://images.amazon.com/images/P/" . $isbn . ".01.THUMBZZZ.jpg");
    if ($Width > 1)
    {
    $book_title = '.01.THUMBZZZ.jpg" alt="'.$post->post_title.'" title="'.$post->post_title.'" />';
    echo c2c_get_custom('isbn-10-sc', '<a href="http://images.amazon.com/images/P/','.01.LZZZZZZZ.jpg" class="thickbox">');
    echo c2c_get_custom('isbn-10-sc', '<img src="http://images.amazon.com/images/P/',$book_title);
    }
    else {
    $isbn = c2c_get_custom('isbn-10');
    list($Width) = @getimagesize("http://images.amazon.com/images/P/" . $isbn . ".01.THUMBZZZ.jpg");
    if ($Width > 1)
    {
    $book_title = '.01.THUMBZZZ.jpg" alt="'.$post->post_title.'" title="'.$post->post_title.'" />';
    echo c2c_get_custom('isbn-10', '<a href="http://images.amazon.com/images/P/','.01.LZZZZZZZ.jpg" class="thickbox">');
    echo c2c_get_custom('isbn-10', '<img src="http://images.amazon.com/images/P/',$book_title);
    }
    else {
    echo '<img src="http://www.tradereadingorder.com/pics/no-cover-thumbnail.gif" alt="No Cover" title="No Cover">';
    } } } }
    ?>

    I feel like there must be some better way to do the conditional statements, but it works! It goes user-uploaded image > Amazon image hardcover > Amazon image softcover > placeholder image.

    Thanks for all your help!!

  12. flick
    Member
    Posted 3 years ago #

    Thanks for sharing your code! I had a look at the post by Matt Brett and he uses the same sort of if-elseif, so as a non-coder, I think it's easier to follow.

    http://mattbrett.com/workshop/2010/wordpress-post-thumbnails-revisited/

  13. Ian
    Member
    Posted 3 years ago #

    ok, so I can do else if instead of just nested "else" tags?

  14. Ian
    Member
    Posted 3 years ago #

    accidental double post, sorry. deleted.

    Curious though, are the else if statements less load on the server?

  15. flick
    Member
    Posted 3 years ago #

    Umm... if I've understood it correctly, it's mostly a case of conditionals written like so: if, else-if, else-if, else, so whatever is the final outcome is else? So no different to what you're doing I suppose.

    I think more knowledgeable people will be able to advise you as to anything else (like Switch maybe?) but just to clarify that I was just saying your code makes sense the way it is, and that other (more code-savvy) people have done the same thing so it should be okay?

  16. Ian
    Member
    Posted 3 years ago #

    yeah it seems to be working. I sometimes worry that my duct taped together code will fall apart under pressure when my site starts getting more traffic, but for now it looks good!

  17. flick
    Member
    Posted 3 years ago #

    I do understand where you're coming from ;)

  18. keesiemeijer
    moderator
    Posted 3 years ago #

    If your code works as you want to, don't change it. the code in the link flick gave you is for theme developers who want to have on the fly image sizes that you didn't specify in the WordPress admin Media panel under Settings > Media or with add_image_size() so their theme works as they want to. If your code works don't change it, it is not much load on the server

  19. Ian
    Member
    Posted 3 years ago #

    It works! But it also worked with a lack of tags, haha. It sometimes seems like things are working perfectly fine, but the code technically has errors in it. i worry they will be problems down the line

    But I'm pretty happy with this right now. I think the nested else statements might be cumbersome, but they're not technically incorrect.

    Thanks again for your help.

  20. flick
    Member
    Posted 3 years ago #

    Thanks for the clarification, keesiemeijer!

Topic Closed

This topic has been closed to new replies.

About this Topic