Jump to content

Oneforeach loop to rule them all - with image size specified


hollyvalero
 Share

Recommended Posts

I came across a foreach example that I was able to customize a bit without several error messages. Yes, I am new. Be kind... coming from Modx.  Basically, 80% of what I need in most web development is: bring back a bunch of fields from THIS parent page or THIS template... and display a limited number... sorted by whatever....  home page features, carousels, masonry... it's everywhere

This is a basic sample for news items on a sample home page - 3 across the bottom.  What I can't seem to incorporate is the ability to say:     $img = $image->size(320,180);

No matter what image size has been uploaded, in the display we want it to be 320 px by 240px. I can do the images alone...  foreach($images as $image) but I can't seem to get that into this foreach and I assume it's because I am using $features at the top ... I'd be happy to call it $pages or $penguin or anything...  I just want to keep the ability to use the same thing in about 3-6 locations within a website by changing the template name... since this is a very common function.

Thank you!

$features = $pages->find("template=newsitem, limit=3, sort=-date");

foreach($features as $feature) {

   echo "<div class='column is-4' >" .
        "<img src='{$feature->image->url}' alt='' />" .
        "<h3 class='title is-4'><a href='{$feature->url}'>{$feature->title}</a></h3>" .
        "<p><span class='date'>{$feature->date}   • </span>" .
        "{$feature->summary}</p>" .
        "</div>";

}

 

 

 

 

 

Link to comment
Share on other sites

This should work:

<?php

$features = $pages->find("template=newsitem, limit=3, sort=-date");

foreach($features as $feature) {
	$img = $image->first()->size(320, 180)->url;
	echo "
		<div class='column is-4' >" .
			"<img src='$img' alt='' />" .
			"<h3 class='title is-4'><a href='{$feature->url}'>{$feature->title}</a></h3>" .
			"<p><span class='date'>{$feature->date} &nbsp; &bull; </span>" .
			"{$feature->summary}</p>" .
		"</div>";
}

You can read more in the docs. On why you should use first() - or not.

  • Like 4
Link to comment
Share on other sites

Welcome to the forums and ProcessWire :). Oh, we are very kind to everyone, especially those coming from the nightmare that is WordPress. Just kidding. Former MODx-er myself. 

Nothing wrong with using the variable $features. What I suspect is that your image field is set to hold (and return) more than one image, i.e. an array. So, calling the resize function on an array won't work. 

Edit Been beaten by others who are faster at typing/thinking...so will stop typing now :-[:P :)

  • Like 4
Link to comment
Share on other sites

1 minute ago, kongondo said:

Nothing wrong with using the variable $features. What I suspect is that your image field is set to hold (and return) more than one image, i.e. an array. So, calling the resize function on an array won't work.  

** Actually the field is a single image field called "image" ... used for news features that have one teaser image... 

Link to comment
Share on other sites

5 minutes ago, arjen said:

This should work:


<?php

$features = $pages->find("template=newsitem, limit=3, sort=-date");

foreach($features as $feature) {
	$img = $image->first()->size(320, 180)->url;
	echo "
		<div class='column is-4' >" .
			"<img src='$img' alt='' />" .
			"<h3 class='title is-4'><a href='{$feature->url}'>{$feature->title}</a></h3>" .
			"<p><span class='date'>{$feature->date} &nbsp; &bull; </span>" .
			"{$feature->summary}</p>" .
		"</div>";
}

You can read more in the docs. On why you should use first() - or not.

 

 

So this gives me:  Call to a member function first() on a non-object (line 7  -- which is the line:

$img = $image->first()->size(320, 180)->url;

And most of my errors seemed to be like this.  The field is a single image field, not a multiple image field.

Link to comment
Share on other sites

We didn't define the $image variable. How about this?

<?php

$features = $pages->find("template=newsitem, limit=3, sort=-date");

foreach($features as $feature) {
	$img = $feature->image->size(320, 180)->url;
	echo "
		<div class='column is-4' >" .
			"<img src='$img' alt='' />" .
			"<h3 class='title is-4'><a href='{$feature->url}'>{$feature->title}</a></h3>" .
			"<p><span class='date'>{$feature->date} &nbsp; &bull; </span>" .
			"{$feature->summary}</p>" .
		"</div>";
}
  • Like 3
Link to comment
Share on other sites

1 minute ago, arjen said:

We didn't define the $image variable. How about this?


<?php

$features = $pages->find("template=newsitem, limit=3, sort=-date");

foreach($features as $feature) {
	$img = $feature->image->size(320, 180)->url;
	echo "
		<div class='column is-4' >" .
			"<img src='$img' alt='' />" .
			"<h3 class='title is-4'><a href='{$feature->url}'>{$feature->title}</a></h3>" .
			"<p><span class='date'>{$feature->date} &nbsp; &bull; </span>" .
			"{$feature->summary}</p>" .
		"</div>";
}

YAY!!  That worked!!!  Thank you!

 

 

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...