View Single Post
Old 02-04-2011, 18:33   #2
Fede 88
Senior Member
 
L'Avatar di Fede 88
 
Iscritto dal: Aug 2004
Cittā: Milano
Messaggi: 1310
Da quel che sono riuscito a capire la parte di codice predisposta per la modifica dovrebbe essere quella finale, ovvero questa:
Codice PHP:
// select the post_type sql for both post pages (post_status = 'static')  
  // and posts (AND post_status = 'publish') 
  // or for just pages or for just posts (the default) 
  // by adding this where criteria, we also solve the problem 
  // of accidentally including images from draft posts. 
  
if ($wp_version '2.1') { 
    if (
$post_type == "both") { 
      
$post_type_sql "AND (post_status = 'publish' OR post_status = 'static')"
    } else if (
$post_type == "pages") { 
      
$post_type_sql "AND post_status = 'static'"
    } else { 
      
$post_type_sql "AND post_status = 'publish'"
    } 

  } else { 
    if (
$post_type == 'both') { 
      
$post_type_sql "AND post_status = 'publish' AND post_type in ('post', 'page')"
    } elseif (
$post_type == 'pages') { 
      
$post_type_sql "AND post_status = 'publish' AND post_type = 'page'"
    } else { 
      
$post_type_sql "AND post_status = 'publish' AND post_type = 'post'"
    } 
  } 

  
// assuming $category_filter is a comma separated list of category ids, 
  // modify query to join with post2cat table to select from only the chosen categories 
  
$category_filter_join  ""
  
$category_filter_sql   ""
  
$category_filter_group ""
  if (
$category_filter != "") { 
    if (
$wp_version >= '2.3') { 
      
$category_filter_join  "LEFT JOIN $wpdb->term_relationships ON $wpdb->posts.ID = $wpdb->term_relationships.object_id LEFT JOIN $wpdb->term_taxonomy ON $wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id"
      
$category_filter_sql   "AND $wpdb->term_taxonomy.term_id IN ($category_filter)";    
      
$category_filter_group "GROUP BY $wpdb->posts.ID"

    } else { 
      
$category_filter_join  "LEFT JOIN $wpdb->post2cat ON $wpdb->posts.ID = $wpdb->post2cat.post_id"
      
$category_filter_sql   "AND $wpdb->post2cat.category_id IN ($category_filter)";    
      
$category_filter_group "GROUP BY $wpdb->posts.ID"
    } 
  } 
   
  
// by default we sort images randomly, 
  // but we can also sort them in descending date order 
  
if ($sort_images_randomly) { 
    
$order_by_sql "rand()"
  } else { 
    
$order_by_sql "$wpdb->posts.post_date DESC"
  } 

  
// query records that contain img tags, ordered randomly 
  // do not select images from password protected posts 
  
$sql "SELECT $wpdb->posts.ID, $wpdb->posts.post_title, $wpdb->posts.post_content, $wpdb->posts.post_excerpt 
          FROM 
$wpdb->posts 
          
$category_filter_join 
          WHERE (post_content LIKE '%<img%' or post_content LIKE '%[gallery]%') 
          AND post_password = '' 
          
$post_type_sql 
          
$category_filter_sql 
          
$category_filter_group 
          ORDER BY 
$order_by_sql"
  
$resultset = @mysql_query($sql$wpdb->dbh); 
   
  if (
$debugging && mysql_error($wpdb->dbh)) print "mysql errors: " mysql_error($wpdb->dbh) . "<br/> SQL: " htmlspecialchars($sql) . "<br/>";; 
  if (
$debugging) print "elligible post count: " . @mysql_num_rows($resultset) . "<br/>";  
   
  
// keep track of multiple images to prevent displaying dups 
  
$image_srcs = array(); 

  
// loop through each applicable post from the database 
  
$image_count 0
  while (
$row mysql_fetch_array($resultset)) { 
    
$post_title     $row['post_title']; 
    
$post_permalink get_permalink($row['ID']); 
    
$post_content   $row['post_content']; 
    
$post_excerpt   $row['post_excerpt']; 
    
$matches = array(); 

    
// find all img tags 
    
preg_match_all("/<img[^>]+>/i"$post_content$matches); 
     
    if (
$show_images_in_galleries) { 
      
// find any gallery attachments 
      
if (strpos($post_content'[gallery]') !== false) { 
        
$sql "SELECT $wpdb->posts.guid, $wpdb->posts.post_excerpt 
                FROM 
$wpdb->posts 
                WHERE post_parent = '" 
$row['ID'] . "' 
                AND post_type = 'attachment' 
                AND post_password = '' 
                AND post_mime_type in ('image/jpeg', 'image/gif', 'image/png')"

        
$resultset2 = @mysql_query($sql$wpdb->dbh); 
        while (
$row2 mysql_fetch_array($resultset2)) { 
          
$matches[0][] = "<img src='" $row2['guid'] . "' alt='" htmlspecialchars($row2['post_excerpt'], ENT_QUOTES) .  "' />"
        } 
      } 
    } 

    
// if there are none, try again,   
    
if (count($matches[0]) == 0) { 
      continue; 
    } 

    
// randomize the array of images in this post 
    
shuffle($matches[0]); 

    
// loop through each image candidate for this post and try to find a winner         
    
foreach ($matches[0] as $image_element) { 
      
// grab the src attribute and see if it exists, if not try again 
      
preg_match("/src\s*=\s*(\"|')(.*?)\\1/i"$image_element$image_src); 
      
$image_src $image_src[2]; 
       
      
// make sure we haven't displayed this image before 
      
if ($image_src == "" || in_array($image_src$image_srcs)) { 
        continue; 
      } 

      
// if a regex is supplied and it doesn't match, try next post 
      
if ($image_src_regex != "" && !preg_match("/" $image_src_regex "/i"$image_src)) { 
        continue; 
      } 

      if (
$image_class_match != "") { 
        
// grab the class attribute and see if it exists, if not try again 
        
preg_match("/class\s*=\s*(\"|')(.*?)\\1/i"$image_element$image_classes); 
        
$image_classes $image_classes[2]; 

        if (
$image_classes == '') { 
          continue; 
        } 

        
$image_classes explode(" "$image_classes); 
        if (!
in_array($image_class_match$image_classes)) { 
          continue; 
        } 
      } 

      
// add img src to array to check for dups 
      
$image_srcs[] = $image_src
          
      
// grab the alt attribute and see if it exists, if not supply default 
      
preg_match("/alt\s*=\s*(\"|')(.*?)\\1/i"$image_element$image_alt); 
      
$image_alt $image_alt[2]; 

      if (
$image_alt == "") { 
        
$image_alt "random image"
      } 

      
$image_html $image_template_html
      
$image_html str_replace("%1"$post_title$image_html); 
      
$image_html str_replace("%2""<a href='$post_permalink'><img src='$image_src' alt='$image_alt$image_attributes /></a>"$image_html); 
       
      if (
$image_alt == 'random image') { 
        
$image_html str_replace("%3"''$image_html); 
      } else { 
        
$image_html str_replace("%3"$image_alt$image_html); 
      } 

      
$image_html str_replace("%4"$post_excerpt$image_html); 

      print 
$image_html

      
$image_count++; 
       
      if (
$image_count == $number_of_images) { 
        return; 
      } else { 
        
// print a linebreak between each successive image 
        
print "$inter_image_html\n"
      } 
       
      
// leave the foreach loop and look for images 
      // in other posts 
      // TODO: if people wanted to display multiple images per post, 
      // we would selectively skip this break 
      
break; 
    } 
  } 

?> 
Purtroppo non capisco quale variabile utilizzare per fare il controllo sul formato dell'immagine (ho intenzione di visualizzare solo immagini con rapporto larghezza/altezza = a 1,5).
Fede 88 č offline   Rispondi citando il messaggio o parte di esso