Conditional Thumbnail Display? Check for thumbnail, if not, display code
-
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?
-
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); ?> ?>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.
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); ?>I think that might work, I’ll try it!
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'; } ?>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'; } ?>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'; } ?>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!
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); } ?>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!!
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/
ok, so I can do else if instead of just nested “else” tags?
accidental double post, sorry. deleted.
Curious though, are the else if statements less load on the server?
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?
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!
The topic ‘Conditional Thumbnail Display? Check for thumbnail, if not, display code’ is closed to new replies.