WordPress is turning into a platform for developing web applications so almost every kind of site you can develop using WordPress and to work with WordPress is pretty easy than the other applications.
Recently, I wanted to develop one slider which display all images from a single post and images are slide into the slider.
As usual, I have used the most common function of WordPress is get_posts function to fetch images from the post and for that I have used ‘post_type’ is equal to attachment.
Now, let’s lay down the WordPress code that does this:
1 2 3 4 5 6 7 8 | $attachments = get_posts( array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'post_parent' => $post->ID, 'exclude' => get_post_thumbnail_id() ) ); |
This will load all images attached to the post into the $attachments variable then you can use a foreach loop on them to get your images display.
Above code display, all the images from a single post and you can use those images for a slider or anything else you want to use. ;).
That’s it.I hope this article give you a good idea of how to get all images of single post/page.
Further Reading:
Auto-tagging in WordPress
Hook to change post URL with external one in WordPress
To Exclude Latest Post from the WordPress Loop
Thanks for reading and feel free to share your thoughts! Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates.
Comments (6)