mammasan Posted May 25, 2010 Share Posted May 25, 2010 How could the following be achieved? Preamble: I have designed a custom Wordpress template from scratch using various sources and books for guidance. If a post has certain custom fields (image_1 thumbnail_1 etc.) applied the main image can be 'swapped' by clicking the thumbnail using javascript. The thumbnails are also used elsewhere in the site on the index page as a teaser for newest posts. I'm wondering how I can edit the code so that if there is only one thumbnail present it wouldn't display on the posts page. (it seems pretty pointless to have a thumbnail of the main image underneath if there's only one) The code below I sourced from a book. I want to see if I can employ something similar to echo "<img src='".$image."' id='image_".$i."' style='display:none;'>"; for the thumbnails. How would I go about this? Please note that php is not my language of choice. The business end of the code is below, if you could point out a good starting point here, or via a tutorial that would be great <div class="image_main"> <?php $number_of_images = 1; for ($i=1; $i<=10; $i++) { $image = get_post_meta($post->ID, 'image_'.$i, true); if ($image && $i == 1) { echo "<img src='".$image."' id='image_".$i."'>"; $number_of_image++; } elseif ($image) { echo "<img src='".$image."' id='image_".$i."' style='display:none;'>"; $number_of_images++; } } ?> </div><!--end of images_main--> <div class="image_thumbs"> <?php for ($i=1; $i<=$number_of_images; $i++) { $image = get_post_meta($post->ID, 'thumbnail_'.$i, true); if ($image) { echo "<a href='javascript:image_switch(".$i.",".$number_of_images.");'><img src='".$image."' id='thumbnail_".$i."'></a>"; } } ?> </div><!--end of image_thumbs--> Link to comment https://forums.phpfreaks.com/topic/202863-using-php-to-only-display-or-hide-an-image/ Share on other sites More sharing options...
andrewgauger Posted May 25, 2010 Share Posted May 25, 2010 Do you acquire the thumbnails from a database query? Why not add a cout(*) field? Link to comment https://forums.phpfreaks.com/topic/202863-using-php-to-only-display-or-hide-an-image/#findComment-1063247 Share on other sites More sharing options...
mammasan Posted May 25, 2010 Author Share Posted May 25, 2010 Although Wordpress is pretty much purely driven by a database (posts, categories, tags etc) the custom field value for the thumbnail is a URL (to the image in the uploads folder), not a database value so I'm not 100% sure on that. Also, I'm afraid you'll have to be gentle with me and explain what you mean by cout(*) Link to comment https://forums.phpfreaks.com/topic/202863-using-php-to-only-display-or-hide-an-image/#findComment-1063253 Share on other sites More sharing options...
andrewgauger Posted May 25, 2010 Share Posted May 25, 2010 I mean extending the sql query. Or better yet, you could execute mysql_num_rows on the resource provided by mysql_query. I don't know the details of wordpress' db interactions. Link to comment https://forums.phpfreaks.com/topic/202863-using-php-to-only-display-or-hide-an-image/#findComment-1063268 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.