Created
June 6, 2012 16:41
-
-
Save habibmac/2883160 to your computer and use it in GitHub Desktop.
WP: Auto extract the first image from the post content
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// AUTOMATICALLY EXTRACT THE FIRST IMAGE FROM THE POST | |
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=$imgEnd+1; | |
} | |
if(stristr($image[$num],'<img')) { echo '<a href="'.$link.'">'.$image[$num]."</a>"; } | |
$more = 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment