pietergoosen

imagesexcertp

Jan 21st, 2014
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. function pietergoosen_excerpt_175_words( $length ) {
  2. return 175;
  3. }
  4. add_filter( 'excerpt_length', 'pietergoosen_excerpt_175_words' );
  5.  
  6. function pietergoosen_get_first_image() {
  7. global $post;
  8. $first_img = '';
  9. if( $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches))
  10. $first_img = $matches [1] [0];
  11.  
  12. return $first_img;
  13. }
  14.  
  15. function pietergoosen_check_if_url_exist($url) {
  16. $headers = wp_get_http_headers($url);
  17.  
  18. if (!is_array($headers)) :
  19. return FALSE;
  20. elseif (isset($headers["content-type"]) && (strpos($headers["content-type"],"image") !== false)) :
  21. return TRUE;
  22. else :
  23. return FALSE;
  24. endif;
  25. }
  26. function pietergoosen_adjust_image_size($image, $alt, $newwidth, $newheight) {
  27. if (!file_exists($image) && !pietergoosen_check_if_url_exist($image)) return '';
  28. list($width, $height, $type, $attr) = getimagesize($image);
  29. if (!$width || !$height) return '';
  30.  
  31. if ($newwidth)
  32. $newheight = intval($newwidth/$width * $height);
  33. else
  34. $newwidth = intval($newheight/$height * $width);
  35. return '<img src="' . $image . '" width=' . $newwidth . ' height='. $newheight . ' alt=' . $alt . '/>';
  36. }
  37.  
  38. <div class="alignleft-excerpt">
  39. <a href="<?php echo pietergoosen_get_first_image() ?>" title="<?php printf( the_title_attribute( 'echo=0' ) ); ?>" rel="lightbox">
  40. <?php echo pietergoosen_adjust_image_size(pietergoosen_get_first_image(), get_the_title(), 250, 0); ?>
  41. </a>
  42. </div>
Advertisement
Add Comment
Please, Sign In to add comment