$8
pull add image href in functions.php
Added some code to the functions.php to pull single images from posts into a template and all working great.
With thanks to
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
Rob Cleaton | 04/06/10 at 4:52pm
| Edit
(2) Possible Answers Submitted...
-

Last edited:
04/06/10
4:59pmMilan Petrovic says: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.Previous versions of this answer: 04/06/10 at 4:59pm
-

Last edited:
04/06/10
5:21pmUtkarsh Kukreti says: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.Previous versions of this answer: 04/06/10 at 5:00pm
- 04/06/10 5:11pm
Rob Cleaton says:It seems it doesn't return anything inside the href tag.
- 04/06/10 5:13pm
Utkarsh Kukreti says:Replace the preg_match line with this one
preg_match('/src\s*=\s*\"([^\"]+)/', $image[$num], $m); - 04/06/10 5:21pm
Rob Cleaton says:Thanks for your time Utkarsh Kukret, thats worked brilliantly
- 04/06/10 5:11pm
This question has expired.
Current status of this question: Completed




