Jump to content

echo "<img src='$img->url'>" does not render the Image


psquaretrader
 Share

Recommended Posts

Can you assist me with why the following code does not display the image; all that show up is a small x inside a small box.  

When I inspect the element in chrome, it gives me the url path, but not the url with the filename (/site/assets/files/1015/).

For the field 'images', I have maximum files allowed set to '1'.

This is my code snippet for the image:

<?php foreach(page()->children as $posts): ?>
	<?php $img = $posts->images;
			 echo "<img src='$img->url'>";	?>
<?php endforeach; ?>

Thanks!

Link to comment
Share on other sites

SamC thank you for the reply.  It didn't seem to make a difference.  Here is the link to the page https://giftsguysreallywant.com/products/.  The images should display on the tiles right below the title.

this is the entire code for the template file:

<?php namespace ProcessWire; 
// This is the template file for main /blog/ page that lists blog post summaries.
// If there are more than 10 posts, it also paginates them. 
?>
<div id='content'>



<div class='uk-child-width-1-2@s uk-child-width-1-2@m uk-grid-match uk-margin-large-bottom' pw-append='content' uk-grid>
	<?php foreach(page()->children as $posts): ?>
	<a class='uk-link-reset' href='<?=$posts->url?>'>
		<div class='uk-card uk-card-default uk-card-hover uk-card-body'>
			
			<h3 class='uk-card-title uk-margin-remove'><?=$posts->title?></h3>
			<?php $img = $posts->images;
			 echo "<img src='{$img->url}'>";	?>
			
		</div>	
	</a>	
	<?php endforeach; ?>
</div>		
</div>

<aside id='sidebar'>
	<?php 
	$categories = pages()->get('/categories/'); 
	echo ukNav($categories->children, [ 'header' => $categories->title ]); 
	?>		
</aside>

 

Link to comment
Share on other sites

6 minutes ago, psquaretrader said:

Thanks LostKobrakia for the response, I'm on my way to work so I haven't had a chance to check it with the code that you provided. I thought that if you had the number of image set to one then it is no longer an array or list

This is what I was thinking ^ shouldn't need first(). I use similar code for my main heading image.

i thought this would work:

<div class='uk-child-width-1-2@s uk-child-width-1-2@m uk-grid-match uk-margin-large-bottom' pw-append='content' uk-grid>
	<?php foreach(page->children as $post): ?>
	<a class='uk-link-reset' href='<?php echo $post->url; ?>'>
		<div class='uk-card uk-card-default uk-card-hover uk-card-body'>
			
			<h3 class='uk-card-title uk-margin-remove'><?php echo $post->title; ?></h3>
			 <img src='<?php echo $post->images->url ?>'>
			
		</div>	
	</a>	
	<?php endforeach; ?>

Maybe check setup > fields > Edit Field: images > details

...and make sure Formatted value is 'Automatic' (so a single item when set to 1 or null if empty).

Link to comment
Share on other sites

Even with the setting enabled there are various places in processwire where output formatting is not enabled and therefore one does still get a list of images (might not be the case here, but still). Also I did check the website and the urls for the images are "/site/assets/files/xxxx/", which is the result of using ->url on the images list instead of a single image. Also a field named "images" should probably never return a single image or it should at least be renamed to image.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...