Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2012
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. <?php
  2. $category_ID = 25;
  3.  
  4. global $wpdb;
  5.  
  6. // get the first 18 posts with an attachment from the database
  7. $myposts = $wpdb->get_results("
  8. SELECT *
  9. FROM $wpdb->posts
  10. LEFT JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
  11. LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
  12. WHERE post_status = 'publish'
  13. AND ID IN (
  14. SELECT DISTINCT post_parent
  15. FROM $wpdb->posts
  16. WHERE post_parent > 0
  17. AND post_type = 'attachment'
  18. AND post_mime_type IN ('image/jpeg', 'image/png')
  19. )
  20. AND $wpdb->term_taxonomy.taxonomy = 'category'
  21. AND $wpdb->term_taxonomy.term_id = '" . $category_ID . "'
  22. ORDER BY RAND() LIMIT 0,18
  23. ");
  24.  
  25. if($myposts){
  26. foreach($myposts as $post) {
  27. setup_postdata($post);
  28. $images = get_children(array(
  29. 'post_parent' => $post->ID,
  30. 'post_type' => 'attachment',
  31. 'post_mime_type' => 'image',
  32. 'orderby' => 'rand'
  33. ));
  34.  
  35. if(!empty($images)){
  36. $ids = array_keys($images);
  37. // link attachement to post
  38. echo '<a href="' . get_permalink($post->ID) . '" >' . wp_get_attachment_image($ids[0], 'thumbnail') . '</a>';
  39. }
  40.  
  41. }
  42. wp_reset_postdata();
  43. }
  44. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement