Jump to content

Two issues... Firstly, I can't figure out how to get variations of one of my images from another page. Second, I need to grab a fixed amount of images, not all of them.


spercy16
 Share

Recommended Posts

These issues should be fairly easy for any intermediate to advanced ProcessWire developer to answer. I'm new to PHP and relatively new to ProcessWire and just need a bit of help. What I'm trying to do is bring in a couple of cards from my Projects page to display on my home page. I finally got the code right to bring in the cards but right now they're using my original images instead of my resized "variations". So firstly, I would like to know how to reference the variations of my images instead of using the original. Secondly, I need to grab only four of the cards from the Project page and not import in all ten. It should be just two small changes to my code to do these things (I would imagine). Here is the code I currently have for that section:

<?php
		// https://processwire.com/api/arrays/
		// check if the array of images has items

		if (count($pages->get("/projects/")->images)) :

			// get array of images from the field
			$images = $pages->get("/projects/")->images;

			$count = 0;

			// iterate over each one
			foreach ($images as $image) :

				$count++;

				$sectionText = $pages->get("/projects/")->get("paragraph_$count");

				$img = $image;

				$buttonCode = $pages->get("/projects/")->get("url_$count");

		?>

			<span id="card<?php echo $count?>" class="card">

				<img class="cardThumb" src="<?php echo $img->url; ?>" alt="<?php echo $image->description; ?> Thumbnail" />

				<div class="cardBody">
                  
					<div class="cardText">
						<h2><?php echo $img->description; ?></h2>
						<?php echo $sectionText; ?>
					</div>

					<div class="primaryBtn">
						<a href="https://www.paypal.com/donate?hosted_button_id=
							<?php echo $buttonCode; ?>
							&source=url">
							<button>
								<i class="fas fa-donate"></i> Donate
							</button>
						</a>
					</div>
                  
				</div>

			</span>

		<?php
			endforeach;
			endif;
		?>

Thanks in advance for any help!

Link to comment
Share on other sites

Hey, first thing that I spotted is that you do a check if there are images, what exactly means there can be one or more images. But you also said you need exactly 4 images. So, what should the code do if there are less then 4 images available? You may do a check if it at minimum has 4 images:

if (4 <= count($pages->get("/projects/")->images))

So, if you would check that there are more than 3 images available, you should use a for loop instead of the foreach loop.

// iterate over first four images
for($i = 0; $i < 4; $i++) :
	$image = $images->eq($i);
    ...

Third thing is to get the variation. You can do it at the point where you copy the $image to the $img variable:

$img = $image->width(200);  // or $image->height(...) or $image->size(...)

 

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