0

Using Add Media I am inserting images in the Post.I want to reterive these only these images on the single post page.I am using get_attached_media function to reterive these images.But it returns an empty array.

 <?php $media_image = get_attached_media( 'image', $post->ID );  //print_r($media_image);?>
                <div class ="single-post-flex-slider">
                    <ul class="slides">
                        <?php foreach($attachments as $image){ 
                            $image_attr = wp_get_attachment_image_src( $image->ID,array(500,500) );
                        ?>
                        <li> <img src="<?php echo $image_attr[0] ?>" width="<?php echo $image_attr[1] ?>" height="<?php echo $image_attr[2] ?>" /></li>
                        <?php } ?>
                    </ul>
                </div>
13
  • Are you doing this inside the loop – Pieter Goosen Jan 6 '15 at 6:44
  • Yes I am using this inside the loop.php with the condition is_single – John Jan 6 '15 at 6:47
  • print_r($media_image) returns an empty array.but the post returns the values in an array – John Jan 6 '15 at 6:54
  • Is there any way to extract images from the post content. – John Jan 6 '15 at 6:58
  • See this post – Pieter Goosen Jan 6 '15 at 7:02
1

The Image added by Add Media is included in the Content.So I have splitted the image from the content.Below is the working code for my Question.

<?php $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches); ?>

<div class ="single-post-flex-slider">
    <ul class="slides">
        <?php for($i =0;$i<$output;$i++){ ?>
        <li> <img src="<?php echo $matches[1][$i]; ?>" width="500px" height="300px" /></li>
        <?php } ?>
    </ul>
</div>
1
  • Remember to accept your own answer when the restrictions has been lifted (normally 2 days) :-) – Pieter Goosen Jan 6 '15 at 9:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.