Posted By

peterbelsky on 05/01/11


Tagged


Versions (?)

Wordpress get gallery images


 / Published in: PHP
 

URL: http://johnford.is/programmatically-pull-attachments-from-wordpress-posts//

  1. // GET THE PATHS
  2.  
  3. // IN FUNCTIONS:
  4. // get all of the images attached to the current post
  5. function aldenta_get_images($size = 'thumbnail') {
  6. global $post;
  7.  
  8. return get_children( array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') );
  9. }
  10.  
  11.  
  12. // IN PAGE
  13. if ($images = aldenta_get_images()) {
  14. foreach ($images as $image) {
  15.  
  16. echo wp_get_attachment_url($image->ID); // attachment url
  17. echo wp_get_attachment_image($image->ID, 'thumbnail'); // thumbnail image
  18. echo $image->post_title; // title
  19. echo $image->post_content; // description
  20. echo $image->post_excerpt; // caption
  21.  
  22. }
  23. }
  24.  
  25.  
  26. GET THE IMAGES
  27.  
  28. // IN FUNCTIONS
  29. // get all of the images attached to the current post
  30. function aldenta_get_images($size = 'thumbnail') {
  31. global $post;
  32.  
  33. $photos = get_children( array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') );
  34.  
  35. $results = array();
  36.  
  37. if ($photos) {
  38. foreach ($photos as $photo) {
  39. // get the correct image html for the selected size
  40. $results[] = wp_get_attachment_image($photo->ID, $size);
  41. }
  42. }
  43.  
  44. return $results;
  45. }
  46.  
  47.  
  48. // IN PAGE
  49.  
  50. $photos = aldenta_get_images('full');
  51.  
  52. if ($photos) {
  53. foreach ($photos as $photo) {
  54. echo "$photo<br />";
  55. }
  56. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: sonia on October 8, 2012

Thanks very much for this code snippet

You need to login to post a comment.