Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

pull add image href in functions.php WordPress

  • SOLVED

Hey,

Added some code to the functions.php to pull single images from posts into a template and all working great.

With thanks to [[LINK href="http://bavotasan.com/tutorials/retrieve-the-first-image-from-a-wordpress-post/"]][[/LINK]]

Code reads:

function getImage($num) {
global $more;
$more = 1;
$link = get_permalink();
$content = get_the_content();
$count = substr_count($content, '<img');
$start = 0;
for($i=1;$i<=$count;$i++) {
$imgBeg = strpos($content, '<img', $start);
$post = substr($content, $imgBeg);
$imgEnd = strpos($post, '>');
$postOutput = substr($post, 0, $imgEnd+1);
$postOutput = preg_replace('/width="([0-9]*)" height="([0-9]*)"/', '',$postOutput);;
$image[$i] = $postOutput;
$start=$imgBeg+$imgEnd+1;
}
if(stristr($image[$num],'<img')) { echo '<a class="image-post" href="'.$link.'">'.$image[$num]."</a>"; }
$more = 0;
}

// add a favicon for your admin
function admin_favicon() {
echo '<link rel="Shortcut Icon" type="image/x-icon" href="'.get_bloginfo('template_directory').'/favicon.ico" />';
}


However, I hope a simple question, at the moment its set to added the permalink to the img href, what should be added to link to the single image url?

Many thanks

Rob

Answers (2)

2010-04-06

Utkarsh Kukreti answers:

Try this code
function getImage($num) {
global $more;
$more = 1;
$link = get_permalink();
$content = get_the_content();
$count = substr_count($content, '<img');
$start = 0;
for($i=1;$i<=$count;$i++) {
$imgBeg = strpos($content, '<img', $start);
$post = substr($content, $imgBeg);
$imgEnd = strpos($post, '>');
$postOutput = substr($post, 0, $imgEnd+1);
$postOutput = preg_replace('/width="([0-9]*)" height="([0-9]*)"/', '',$postOutput);;
$image[$i] = $postOutput;
$start=$imgBeg+$imgEnd+1;
}
if(stristr($image[$num],'<img')) {
preg_match('/src\s*=\s*\"(^\")+/', $image[$num], $m);
$imglink = $m[1];
echo '<a class="image-post" href="'.$imglink.'">'.$image[$num]."</a>";
}
$more = 0;
}

// add a favicon for your admin
function admin_favicon() {
echo '<link rel="Shortcut Icon" type="image/x-icon" href="'.get_bloginfo('template_directory').'/favicon.ico" />';
}


Please post if there are any errors/problems.


Rob Cleaton comments:

It seems it doesn't return anything inside the href tag.


Utkarsh Kukreti comments:

Replace the preg_match line with this one

preg_match('/src\s*=\s*\"([^\"]+)/', $image[$num], $m);


Rob Cleaton comments:

Thanks for your time Utkarsh Kukret, thats worked brilliantly

2010-04-06

Milan Petrovic answers:

This function will grab image URL from first image found in the post using regular expressions:

function get_image_from_text($text) {
$imageurl = "";
preg_match('/<\s*img [^\>]*src\s*=\s*[\""\']?([^\""\'>]*)/i', $text, $matches);
$imageurl = $matches[1];
return $imageurl;
}


$matches contains all found images, and function by default returns first one.