ngrmm Posted July 26, 2017 Share Posted July 26, 2017 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 More sharing options...
SamC Posted July 26, 2017 Share Posted July 26, 2017 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 More sharing options...
ngrmm Posted July 26, 2017 Author Share Posted July 26, 2017 the missing '}' was a incomplete copy/paste however i found another solution which works fine: $n = $article->view; $thumbs = $article->images->find("limit=$n"); 1 Link to comment Share on other sites More sharing options...
SamC Posted July 26, 2017 Share Posted July 26, 2017 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 More sharing options...
ngrmm Posted July 28, 2017 Author Share Posted July 28, 2017 @SamC my option-titles are words and not numbers. i don't know if that will work. Link to comment Share on other sites More sharing options...
SamC Posted August 2, 2017 Share Posted August 2, 2017 On 28/07/2017 at 9:53 AM, ngrmm said: @SamC my option-titles are words and not numbers. i don't know if that will work. I'm not sure either, but did you try it? Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now