There are several ways to load and handle image attachment in WordPress post. I introduce here some easy approaches to get things done.
1. Retrieve all image attachments.
To achieve this, we can use get_posts or get_children functions. In this case, I will show get_children().
$attachments = get_children(array('post_parent' => $designer_post->ID, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'post_date')); //this will have all attachment post objects to be stored in an array ($attachments);
2. Retrieve featured image only.
To achieve this, we can use combined function to do so.
$profile_image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); //use get_post_thumbnail_id() to return the featured image attachment id //use wp_get_attachment_image_src to return the featured image information in an array. //$profile_image is storing the featured image url.
3. Retrieve all images excluding featured image
To achieve this, we can use get_posts function to exclude the featured image in the array argument.
//specify the array argument first. $attachment_args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_parent' => $post->ID, 'exclude' => get_post_thumbnail_id(), //exclude the featured image by id 'orderby' => 'post_date', 'order' => 'ASC' ); $attachments = get_posts( $attachment_args );
Hope this helps your daily web development.