WordPress-Powered Portfolios: Gallery


/ Published in: PHP
Save to your folder(s)

For use within The Loop, in your portfolio item page template.


Copy this code and paste it in your HTML
  1. <?php
  2. // http://tylersticka.com/2009/09/wcpdx09/
  3. // Retrieve custom meta for this item
  4. $fields = get_post_custom();
  5.  
  6. // If the gallery key exists, let's show some gallery functionality
  7. if (isset($fields['gallery'])) {
  8.  
  9. // Split the comma-separated gallery meta value into an array
  10. $images = split(',',$fields['gallery'][0]);
  11.  
  12. // Grab the current WordPress page and use it as the current
  13. // gallery item (defaults to 1)
  14. $current = ($paged == '') ? 1 : $paged;
  15.  
  16. // Grab the current image from the array
  17. $image = $images[$current-1];
  18.  
  19. // Display the current gallery image with intelligent alt text
  20. ?>
  21. <div id="gallery">
  22. <img src="<?php echo $image; ?>" alt="<?php echo "Image $current of ".count($images); ?>" />
  23. <?php
  24. // Only show "next" and "back" controls if there's more
  25. // than one gallery item
  26. if (count($images) > 1) {
  27. ?>
  28. <ul>
  29. <?php if ($current > 1) { ?><li class="prev"><a href="<?php the_permalink() ?>page/<?php echo $current-1; ?>">Previous Image</a></li><?php } ?>
  30. <?php if ($current < count($images)) { ?><li class="next"><a href="<?php the_permalink() ?>page/<?php echo $current+1; ?>">Next Image</a></li><?php } ?>
  31. </ul>
  32. <?php } ?>
  33. </div>
  34. <?php } ?>

URL: http://tylersticka.com/2009/09/wcpdx09/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.