Horje
Get Post Attachments - Wordpress Solution
[Solved] - Get Post Attachments - Wordpress Solution
Try following codes
I'm using the following code in my functions.php file:
<?php
function get_images($size = 'thumbnail') {
	global $post;

	return get_children( array('post_parent' => get_the_ID(), 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') );
}

if ($images = get_images()) {
	foreach ($images as $image) {
		$imgtitle = $image->post_title;
		$imgurl =  wp_get_attachment_url($image->ID);
	}
}
?>
And this code inside my loop:
<?php 
					$photos = get_images('full');
					if ($photos) 
					{
						foreach ($photos as $photo) 
						{
							echo('<div class="image"><img src="'.$imgurl.'" /><span class="title">'.$imgtitle.'</span></div>');
						}
					}
				?>
There are two images attached to the post I'm working with, and this code ($imgurl) returns the same image twice. Obviously I want it to return the URL for each image...

Solution - 1

<?php 

	$photos = get_images('full');

	if ($photos) 
	{

		foreach ($photos as $photo) 

		{
			echo('<div class="image"><img src="'.wp_get_attachment_url($photo->ID).'" /><span class="title">'.$photo->post_title.'</span></div>');

		}

	}

?>

Date: 2022-10-21 05:22:46
Category: Wordpress
More for You