cristiansans (owner)

Revisions

gist: 183991 Download_button fork
public
Public Clone URL: git://gist.github.com/183991.git
Embed All Files: show embed
wp image functions #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//return the first image, <img/> tag and all.
function first_image($width=100,$height=100,$zoomcrop=1){
$timthumb_url = get_first_image($width, $height, $zoomcrop);
if(isset($timthumb_url)){
?>
<a href="<?php the_permalink()?>" class="thumbnail" title="Links to: <?php the_title()?>">
<img src="<?php echo $timthumb_url?>" alt="<?php the_title() ?>" class="thumbnail"/>
</a>
<?php
}
}
 
//return just the src for the first image whether that be in the content or an attachment
function get_first_image($width=100,$height=100,$zoomcrop=1){
$tn_src = get_first_content_image();
if (!isset($tn_src)) //no image? try getting an attached image instead (native media galleries inserted into posts use attachments)
$tn_src = get_first_attachment();
 
if(isset($tn_src))
$timthumb_url = get_bloginfo('template_url').'/scripts/timthumb.php?src='.$tn_src.'&h='.$height.'&w='.$width.'&zc='.$zoomcrop;
else
unset($timthumb_url);
 
return $timthumb_url;
}
 
//return just the src for the first image buried in the post's textual content.
function get_first_content_image() {
global $post, $posts;
$first_img = '';
$url = get_bloginfo('url');
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
 
$not_broken = @fopen("$first_img","r"); // checks if the image exists
if(empty($first_img) || !($not_broken)){ //Defines a default image
unset($first_img);
}else{
$first_img = str_replace($url, '', $first_img);
}
return $first_img;
}
 
//return just the src for the first image attachment
function get_first_attachment(){
$querystr =
"
SELECT
wp_posts.post_excerpt AS 'imageTitle'
, wp_posts.guid AS 'imageGuid'
FROM
wp_posts
WHERE
wp_posts.post_parent = ".get_the_ID()."
AND wp_posts.post_type = \"attachment\"
ORDER BY \"menu_order\"
LIMIT 1
";
global $wpdb;
$url = get_bloginfo('url');
 
$post_item = $wpdb->get_row($querystr);
$first_attachment = $post_item->imageGuid;
 
$not_broken = @fopen("$first_attachment","r"); // checks if the image exists
if(empty($first_attachment) || !($not_broken)){ //Defines a default image
unset($first_attachment);
}else{
$first_attachment = str_replace($url, '', $first_attachment);
}
 
return $first_attachment;
}