Jump to content

images array getting items


ngrmm
 Share

Recommended Posts

i'm using an option-field ($page->number) and slice() fot outputting different number of thumbs in each article
echo $n works fine, it outputs 1 or 2 or 3 …

but when i use slice(0, $n), i'm getting the first item.
did i forgot something?

	$articles = $pages->find("template=project, sort=-date ");
	foreach ($articles as $article) {
		$n = $article->number; //option-field 1 or 2 or 3 or 4 …
		$thumbs = $article->images->slice(0, $n);
			foreach($thumbs as $thumb) {
				echo "<img src='$thumb->url' >";
				}

 

Link to comment
Share on other sites

I can only see a missing '}' at the end to close the first loop (presuming an incomplete copy/paste), the rest looks ok to me. I just tried slice in something similar and works fine.

// home.php with images field called 'images'
<?php
    $toRender = $page->images->slice(0, 2); // 2 of 4 available images
    foreach($toRender as $image) {
        echo "<img src='{$image->url}' />";
    } 
?>

Be interested to see what's wrong here.

Link to comment
Share on other sites

Out of curiosity, would this work?

$articles = $pages->find("template=project, sort=-date ");
	foreach ($articles as $article) {

// from https://processwire.com/api/modules/select-options-fieldtype/#outputting-selected-options-on-a-page

		$n = $article->number->title; // <<<< the value of the selected option

		$thumbs = $article->images->slice(0, $n);
			foreach($thumbs as $thumb) {
				echo "<img src='$thumb->url' >";
				}
}

 

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...