use the the_post_thumbnail In WordPress

How to display attached images of posts

Many times people create bigger post or article. Many wp developers asks me many questions daily. Most commonly I asked about showing the all the post images as slide show or only thumbs. Using following code you can fetch the wordpress thumbnails. You just need to put following code into your functions.php file.

How to display attached images of posts

Using following code you can display attached images of posts. You just need to put following code into your functions.php file and you will be done.

functions.php file you can find in your wordpress theme folder. Put following function in that file.

function show_all_thumbs() {
global $post;
$post = get_post($post);

/* image code */
$images =& get_children( 'post_type=attachment&post_mime_type=image&output=ARRAY_N&orderby=menu_order&order=ASC&post_parent='.$post->post_parent);
if($images){
foreach( $images as $imageID => $imagePost ){

unset($the_b_img);
$the_b_img = wp_get_attachment_image($imageID, 'thumbnail', false);
$thumblist .= '<a href="'.get_attachment_link($imageID).'">'.$the_b_img.'</a>';

}
}
return $thumblist;
}

In index.php or single.php you can use following code.

<?php echo show_all_thumbs(); ?>

Using above code you will be able to show or display all the attached images of the posts.

Published by

Purab

I am Purab from India, Software development is my profession and teaching is my passion. Programmers blog dedicated to the JAVA, Python, PHP, DevOps and Opensource Frameworks. Purab's Github Repo Youtube Chanel Video Tutorials Connect to on LinkedIn

2 thoughts on “How to display attached images of posts”

  1. I have a situation where I have a bunch of photos attached to a post. I can get ALL the thumbnails to show up and link to my “lightbox”, BUT I want to limit the thumbs that display on the page.

    For instance, I might have 30 images attached to the post. But I only want to show the first 5 images on the page. BUT when I click one of the thumbnails I want to have ALL 30 images in the lightbox. Any clue on how to achieve this?

    Thanks for any help you can lend. I’m at my wits end here.
    Dave

Leave a Reply

Your email address will not be published.