Advertisement
Guest User

ulfben

a guest
Jan 2nd, 2010
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.97 KB | None | 0 0
  1. /*refactored version of http://pastebin.com/f5cdfcd4c */
  2.  
  3. /*returns one image per (published & public) post.
  4. use params to control the number of posts, the categories, the sort order and image size.
  5.     @returns array(array('post_url', 'post_title','post_id','img_src'), [...])*/
  6. function ulfben_get_post_images($number_of_posts = 5,
  7.                         $category_filter         = '66', //comma delimited list of category IDs
  8.                         $order_by            = 'RAND()', //{$wpdb->posts}.post_date DESC or what have you.
  9.             $size = 'thumbnail'){//'thumbnail', 'medium', 'large' or 'full' or an array of width and height, e.g. array(32,32)  
  10.     global $wpdb;
  11.     $limit = (is_int($number_of_posts) && $number_of_posts > 0) ? $number_of_posts : 1; //select 1 if the parameter seems stupid.
  12.     $query="SELECT {$wpdb->posts}.post_parent, {$wpdb->posts}.ID
  13.             FROM {$wpdb->posts}            
  14.             INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.post_parent = {$wpdb->term_relationships}.object_id)
  15.             INNER JOIN {$wpdb->term_taxonomy} ON ({$wpdb->term_relationships}.term_taxonomy_id = {$wpdb->term_taxonomy}.term_taxonomy_id)
  16.             WHERE {$wpdb->posts}.post_type = 'attachment'
  17.             AND {$wpdb->term_taxonomy}.taxonomy = 'category' AND {$wpdb->term_taxonomy}.term_id IN ('{$wpdb->escape($category_filter)}')
  18.             AND {$wpdb->posts}.post_password = ''
  19.             AND {$wpdb->posts}.post_mime_type IN ('image/jpeg', 'image/gif', 'image/png')                          
  20.             GROUP BY {$wpdb->posts}.post_parent
  21.             ORDER BY {$order_by}                   
  22.             LIMIT {$limit};";
  23.     $results = $wpdb->get_results($query);
  24.     $selection = array();
  25.     foreach($results as $result){
  26.         $parent_post_id = $result->post_parent;
  27.         $image_id = $result->ID;
  28.         $imgurl = wp_get_attachment_image_src($image_id, $size);
  29.         if($imgurl === false){continue;} //the image doesn't exist in the requested size!
  30.         $imgurl = $imgurl[0];
  31.         $post_url = get_permalink( $parent_post_id );
  32.         $post_title = $wpdb->get_var("SELECT {$wpdb->posts}.post_title                             
  33.                         FROM {$wpdb->posts}
  34.                         WHERE {$wpdb->posts}.ID = {$parent_post_id}
  35.                         LIMIT 1;"
  36.                     ); 
  37.         $selection[] = array('post_url'=>$post_url, 'post_title' => wp_specialchars($post_title),'post_id'=>$parent_post_id, 'img_src'=>$imgurl);          
  38.     }  
  39.     return $selection;
  40. }
  41.  
  42. function ulfben_print_post_images($number_of_posts = 5, //number of posts
  43.                      $image_attributes         = '',                      
  44.                                  $category_filter          = '66', //comma delimited list of category IDs
  45.                                  $order_by             = 'RAND()', //{$wpdb->posts}.post_date DESC or what have you.
  46.                      $size = 'thumbnail'){//'thumbnail', 'medium', 'large' or 'full' or an array of width and height, e.g. array(32,32)  
  47.     $images = ulfben_get_post_images($number_of_posts, $category_filter, $order_by, $size);
  48.     foreach($images as $image){
  49.         echo "<a href='{$image['post_url']}' title='{$image['post_title']}'><img src='{$image['img_src']}' alt='{$image['post_title']}' {$image_attributes}/></a>";
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement