Jump to content

Paginating news page


SamC
 Share

Recommended Posts

I've got a news page set up now and wanted to add some pagination to a set limit, say 10 articles. The current code is like so:

// news-index.php template
<?php

    $entries = $page->children();
    $created_by = $page->createdUser->displayName;
?>

<?php foreach ($entries as $entry): ?>

    <?php $publish_date = date('d/m/y', $entry->created);

    if ($entry->mainImage) {
        $image = $entry->mainImage->size(768, 280, 'center');
        $thumbnail = $image->url;
    }
    else {
        $thumbnail = '';
    }

    ?>

    <div class='news-item zebra'>
        <div class='news-column-left'>
            <div class='news-index-image-wrapper' style='background-image: url("<?php echo $thumbnail ?>");'>
                <h2>
                    <a href='<?php echo $entry->url ?>'><?php echo $entry->title; ?></a>
                </h2>
            </div>
        </div>

        <div class='news-column-right'>
            <div class='entry-info'>
                <span class='fa fa-calendar' aria-hidden='true'></span> Posted by <span class='entry-highlight'><?php echo $created_by; ?></span> on <span class='entry-highlight'><?php echo $publish_date; ?></span>
            </div>

            <?php echo $entry->summary ?>

            <?php if ($entry->body): ?>
                <p class='read-more'><a href='<?php echo $entry->url ?>'>Read full article <span class='fa fa-arrow-circle-right' aria-hidden='true'></span></a></p>
            <?php endif; ?>

        </div>
    </div>
<?php endforeach; ?>

I was reading this https://processwire.com/docs/admin/setup/templates/#allow-page-numbers and can see there is a 'URLS tab -> allow page numbers' setting for my template in the admin area.

Not really sure what to do here, whether I use this setting or do something manually. The news-index.inc list the news articles, and a news-entry.inc template outputs the individual ones. It all currently works but of course the list will just keep growing! Any hints are appreciated, thanks.

 

 

Link to comment
Share on other sites

Awesome, thanks. But I'm using:

// children of current page /news/
$entries = $page->children();

rather than:

$entries = $pages->find("template=news-entry, limit=10, sort=-sort");

Should I be using the latter in order for renderPager() to work?

 

--EDIT--

Also, do I need to check the 'use page numbers' on news-index.inc template or the actual news entries template (news-entry.inc)?

Link to comment
Share on other sites

Ok, so the page numbers checkbox is for news-index.php. The following works:

<?php

    $entries = $pages->find("template=news-entry, limit=10, sort=-sort");
    $pagination = $entries->renderPager();
    $created_by = $page->createdUser->displayName;
?>

<?php foreach ($entries as $entry): ?>

    <?php $publish_date = date('d/m/y', $entry->created);

    if ($entry->mainImage) {
        $image = $entry->mainImage->size(768, 280, 'center');
        $thumbnail = $image->url;
    }
    else {
        $thumbnail = '';
    }

    ?>

    <div class='news-item zebra'>
        <div class='news-column-left'>
            <div class='news-index-image-wrapper' style='background-image: url("<?php echo $thumbnail; ?>");'>
                <h2>
                    <a href='<?php echo $entry->url; ?>'><?php echo $entry->title; ?></a>
                </h2>
            </div>
        </div>

        <div class='news-column-right'>
            <div class='entry-info'>
                <span class='fa fa-calendar' aria-hidden='true'></span> Posted by <span class='entry-highlight'><?php echo $created_by; ?></span> on <span class='entry-highlight'><?php echo $publish_date; ?></span>
            </div>

            <?php if ($entry->summary) {
                echo $entry->summary;
            }
            ?>

            <?php if ($entry->body): ?>
                <p class='read-more'><a href='<?php echo $entry->url; ?>'>Read full article <span class='fa fa-arrow-circle-right' aria-hidden='true'></span></a></p>
            <?php endif; ?>

        </div>
    </div>
<?php endforeach; ?>

<?php echo $pagination; ?> 

But can this be applied to $entry->children() or will that just always list all the children on the same page? Thanks.

 

Link to comment
Share on other sites

You need to enable the "use page numbers" checkbox on the template on which you want to pagination to be shown. 

Could you clarify what you mean? You want pagination on the output of the children too?

Link to comment
Share on other sites

1 minute ago, arjen said:

You need to enable the "use page numbers" checkbox on the template on which you want to pagination to be shown. 

Could you clarify what you mean? You want pagination on the output of the children too?

Got it working now with my updated code above thanks.

What I mean is this:

// sorts news articles in same order as they are in admin area, each page will be 10 articles
<?php
    $entries = $pages->find("template=news-entry, limit=10, sort=-sort");
    $pagination = $entries->renderPager();

    echo $pagination;
?>

This works. BUT, my original code didn't use 'find', it listed the children of the current page which is site.com/news/.

So, I had:

<?php
    $entries = $page->children();
    $pagination = $entries->renderPager();

    echo $pagination;
?>

...which just lists ALL the child pages on a single page, no pagination.

How would you get pagination to work when using children(), how do you limit to 10? Is this even possible?

 

Link to comment
Share on other sites

14 minutes ago, SamC said:

How would you get pagination to work when using children(), how do you limit to 10? Is this even possible?

You can use selectors inside children(), eg.

    $entries = $page->children("limit=10, sort=-sort");
    $pagination = $entries->renderPager();

    echo $pagination;
  • Like 2
Link to comment
Share on other sites

  • 2 weeks later...
On 11/6/2016 at 3:43 PM, adrian said:

You can use selectors inside children(), eg.


    $entries = $page->children("limit=10, sort=-sort");
    $pagination = $entries->renderPager();

    echo $pagination;

Thanks :)

  • 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

  • Recently Browsing   0 members

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