Limit ForEach Loop to 4 results

Permalink
Hi, this may be a simple fix, but I can't seem to find any documentation on this. I have a custom block that iterates out pages and files based on specific attributes. Working great, but I would like to limit the loop output to 2 results at a time with a "View All" feature, which might expand collapse the rest out, or link to a page feed, not sure yet. But for the meantime, a loop that echoes 2 results.

<?php foreach($pages as $page) { ?>
            <?php $title = $th->entities($page->getCollectionName());?>
        <div class="feed-item">
            <h4><?php echo $title ?></h4>
            <?php $description = $page->getCollectionDescription();?>
            <?php $description = $th->shortenTextWord($description, 60); ?>
            <p><?php echo $description; ?></p>
            <p><a href="<?php echo \URL::to($page); ?>">Read More</a><p/>
        </div>


Here is an example of my loop and currently, I have two loops in action this is one and the other will need to be reduced to 2, thus resulting in 4 results in total.

Will I need to set a count variable and then integrate an if else statement? or is there a simple display <= 2 items?

I welcome any suggestions.

Thanks.

 
hutman replied on at Permalink Reply
hutman
Is this a PageList custom template or? What type of block are you dealing with?
BenSegniPH replied on at Permalink Reply 1 Attachment
Hi, in truth, it's a bit of both as the requirements are for quite a few types of content. So the custom block I have built uses pages, files and custom thumbnails and is built on the top of a custom category attribute. The block switches the content based on the category ID of the page. Here is an attached screenshot of the block in action.

I have this working, but the risk we have is that the content list will get massive and we need to shorten the results to a max of 4 posts.

I just can't seem to find an example of how this has been implemented in the past. Or I'm possibly looking in the wrong place.
hutman replied on at Permalink Reply
hutman
Can you show the code you use to get the $pages? You should be able to limit those results easily.
BenSegniPH replied on at Permalink Reply
Sure, excuse the coding, it may appear pretty rudimentary, as I'm not an experienced PHP guy.

case 1: ?>
    <?php $pl->filterByCategoryType(2); ?>
        <?php $pages = $pl->getResults(); ?>
        <h2 class="feed-title">Product Sheets</h2>
        <?php foreach($pages as $page) { ?>
            <?php $title = $th->entities($page->getCollectionName());?>
        <div class="feed-item-wrapper">
            <?php
                if ($page->getAttribute('thumbnail')) {
                    $image = $page->getAttribute('thumbnail')->getVersion()->getRelativePath();
                }
            ?>
            <img src='<?php echo $image ?>' />
            <div class="feed-content">
                <h4><?php echo $title ?></h4>
hutman replied on at Permalink Best Answer Reply
hutman
If you change it to be like this it should just get you the first 4 results and not the entire list.

<?php 
$pagination = $pl->getPagination();
$pagination->setMaxPerPage(4)
$pages = $pagination->getCurrentPageResults();
?>
BenSegniPH replied on at Permalink Reply
Thanks Hutman!!! Working just the way I need it to, thanks so much!!!