How do I get specific images from my post? (i.e. get image 1, 2, or 3)
-
Hi, I’m new to wordpress, and have quickly got stumped…i’m coming from HTML/CSS only and PHP is a challenge : /
I have imported and adapted an existing (non-wordpress) site, and I have a super nice slider that I am trying to call each of my post images into a ‘<div>’ individually (I am not using the ‘ul’ ‘li’ model for a slider).
(i’m sure I can get a slider plugin, but I need to stay true to my existing design…and in general would like to learn this).
I found the following code that seems to work in part – but consistently shows only the first image, whether I choose ‘1’, ‘2’, or ‘3’ etc
I have this in my function.php:
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 ‘‘.$image[$num].”“; }
$more = 0;
}and this in single.php within the loop:
getImage(‘2’);It seems like there must be a simple solution – it calls the first image…but the first image only…what do I need to adapt to get each image?
Help much, much appreciated!!!!
(ps I am enjoying WordPress land after many years of being scared to put my toe in)
- The topic ‘How do I get specific images from my post? (i.e. get image 1, 2, or 3)’ is closed to new replies.