You need to add icons to your search results, archives or simply to enable the Facebook Open Graph protocol on your blog site. This task can be accomplished by extracting the URL of the first image, used in post. If you follow certain conventions, this can always be the post or page thumbnail. I have seen many solutions to this relatively simple problem. Unfortunately most of them are incorrect. Here is the right PHP code to get the first image URL from the post content.
<?php
class com_bul7_wp_utils {
// ...........
public static function getFirstPostImage() {
global $post;
if (!preg_match('/
post_content, $matches)) {
return NULL;
}
return $matches[2];
}
// ...........
}
?>
The core of this technique is the regular expression that matches the first image in the post content.
I haven’t used the preg_match_all function, but preg_match, because I need only the first matching tag.

Add A Comment
You must be logged in to post a comment.