Last Updated on November 11, 2023 by Mike Kipruto
To achieve we are going to use the WordPress wp_get_attachment_image()
function.
To display all of the images and titles associated with a specific page as a list of bullets, use the following:
PHP
<ul>
<?php
// Check if there are posts
if ( have_posts() ) : while ( have_posts() ) : the_post();
// Get attachments for the current post
$attachments = get_posts( array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID
) );
// If there are attachments
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
?>
<li>
<?php
// Display the attachment image with 'full' size
echo wp_get_attachment_image( $attachment->ID, 'full' );
?>
<p>
<?php
// Apply filters to the attachment title and display it
echo apply_filters( 'the_title', $attachment->post_title );
?>
</p>
</li>
<?php
}
}
endwhile;
endif;
?>
</ul>
This code generates a list of all image attachments associated with each post.