I'm pretty new with PHP, and all I've learned I did it through the blogging platform WordPress. I hope I'll keep in this beyond WordPress, because it's pretty attracting to me.
By now I have the following issue : when I call for a post, I get, as usual, all of the images I attached to it. But I'd like to retrieve only the first one.
I found this code :
Code: Select all
<?php
$szPostContent = $post->post_content;
$szSearchPattern = '~<img [^\>]*\ />~';
preg_match_all( $szSearchPattern, $szPostContent, $aPics );
$iNumberOfPics = count($aPics[0]);
if ( $iNumberOfPics > 0 ) {
for ( $i=0; $i < $iNumberOfPics ; $i++ ) {
echo $aPics[0][$i];
};
};
?>I'm sure it's a question of syntax : how can I manage those pretty '>' and '0' and 'i++' to get just the "first" image ?
Thanks very much if you can help.