Jump to content

Using php to only display or hide an image


mammasan

Recommended Posts

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-->

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(*)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.