How to get url of image in post
-
This is one of those that drive me crazy.
Below is the code I’m using to get the image (jpg) from a post. Every post has just one image. The code below is living in single.php and the return value is the last image existing in attachments.
The problem is; If I go to another post it will still returns the last image in attachemnts. It seems that it is not recognizing the post_parent value since it is giving me the last image of all images existing in each post.
Please, help me out if you have some experience with this functions. Thank you,
<?php
//echo “id ” . the_ID();
$arrImages =& get_children( ‘post_parent=’ . the_ID() . ‘&post_type=attachment&numberposts=1&post_mime_type=image’ );
//echo “in”;
if ( empty($arrImages) )
{
$attachment_url = “#”;
}
else
{
foreach ( (array) $arrImages as $arrImage )
{
//$attachment_url =& wp_get_attachment_url( $arrImages->ID );
//$attachment_url = $arrImages[0];$attachment_url = wp_get_attachment_url( $arrImage->ID );
echo $attachment_url;
}}
?>
- The topic ‘How to get url of image in post’ is closed to new replies.