Forum

Thread tagged as: Question

Order pages by pageModified

Is there a way to order pages by pageModified? Client wants a list of pages and modified times so they can check which pages are old and need an update.

I have tried this;

            <?php
            perch_pages_navigation(array(
                'from-path'             => '/',
                'hide-extensions'       => true,
                'template'              => 'updated.html',
                'siblings'              => false,
                'add-trailing-slash'    => true,
                'include-hidden'        => true,
                'use-attributes'        => true,
                'sort'                  => 'pageModified',
                'sort-order'            => ASC
            ));
            ?>

But I don't believe sort works with perch_pages_navigation.

I could get a raw array and perhaps sort myself using PHP, but wondered if there was a simplier Perch way to do this?

Thanks, Terry

Terry Upton

Terry Upton 0 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

I don't think there is.

Hi Terry,

I have been trying to do exactly the same thing as you and I have come up with the following solution: ` <?php

     //Return the array of pages to an array using skip-template
     $pages = perch_pages_navigation(array(
        'skip-template'        => true,
        'include-hidden'        => true,
     ));

     //print_r($pages);

     //Compare pageModified dates and order then in ascending '>' order
     function cmp($a, $b){

        $aTime = strtotime ($a ["pageModified"]);
        $bTime = strtotime ($b ["pageModified"]);

        if ($aTime == $bTime) {
           return 0;
        }
        return ($aTime > $bTime) ? -1 : 1;

     };

     uasort($pages, 'cmp');

     //Limit the number of returned items to 9
     $pages = array_slice ($pages, 0, 9);

     ?>
  <ul>
     <?php

        //Render out your list
        foreach($pages as $page):
     ?>
     <li>
        <a href="<?=$page['pagePath']?>"> <?=$page['pageNavText']?> - <?=$page['pageModified']?></a>
     </li>
     <?php endforeach ?>
  </ul>

`

Hope this helps.

Hope this helps

Thanks Richard. I had actually solved this before Christmas and here is the solution I went with.

    <nav class="nav--updated"  role="navigation">
        <?php
            $results = perch_pages_navigation(array(
                'from-path'                 => '/',
                'hide-extensions'           => true,
                'skip-template'             => true,
                'siblings'                  => false,
                'add-trailing-slash'        => true,
                'include-hidden'            => true,
                'use-attributes'            => true,
                'flat'                      => true
            ));


            $sort_array = array();
            foreach($results as $result) {
                $sort_array[$result['pageModified'].$result['pageID']] = $result; //check if pageId is correct
            }

            krsort($sort_array);

            foreach($sort_array as $result) {
            ?>
                <li>
                    <a href="<?php echo $result['pagePath']; ?>">
                    <?php echo $result['pageNavText']; ?> <span class="updated">(Updated: <?php echo strftime('%A, %e %B %Y', strtotime($result['pageModified']));  ?>)</span>
                    </a>
                </li>
            <?php
            }
        ?>
        </ul>
    </nav>