pixeltocode
pixeltocode

Reputation: 5308

get attached image in Wordpress

i can use <?php the_content();?> to get all post contents but how can i show only the image attached to the post?

Thanks

Upvotes: 0

Views: 6797

Answers (2)

Sarfraz
Sarfraz

Reputation: 382616

You may find these readings useful:

Upvotes: 2

linuxbastard
linuxbastard

Reputation: 21

You can create a query that lists all the posts with images and then use wp_get_attachment_url, wp_get_attachment_image, or wp_get_attachment_link. The code below gets all image attachments from all posts then displays them in a loop (i.e. for an image gallery).

<ul>
        <?php
        $wp_query = new WP_Query(array('post_type' => 'attachment','post_mime_type' =>'image','post_status' => 'inherit', 'posts_per_page' => '20', 'orderby' => 'date','order' => 'DESC'));
        if($wp_query->have_posts()) : while ($wp_query->have_posts()) : $wp_query->the_post();?>
                <li>
                <a href="<?php echo wp_get_attachment_url($post->ID); ?>"
                class="post-images"
                title="<?php the_title(); ?>" >
                <img width="100" height="100" src="<?php echo wp_get_attachment_thumb_url( $post->ID);?>" /></a>
                </li>
        <?php endwhile; ?>
        </ul>

Upvotes: 2

Related Questions