开发者

get attached image in Wordpress

i can use <?php 开发者_如何学Gothe_content();?> to get all post contents but how can i show only the image attached to the post?

Thanks


You may find these readings useful:

  • Get Images Attached to a Post
  • Get The Image WordPress Plugin


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜