Order news posts, but don't show them in the navigation

Have a site with multiple pages and subpages. Normally I want to show the first level of subpages for every page (link in the starter kit theme). Only for the news page I don’t want to show the posts in the navigation (because they are short and gathered on the news page). A solution would be to make the posts invisible pages, but then I can’t see how I order the posts. Is there a nice solution to not show the posts in the navigation but order them (by publish date)?

Thank you for your help.

Well for the first problem, you can use an if-statement to exclude the submenu for posts from the navigation.

<nav role="navigation">

  <ul class="menu cf">
    <?php foreach($pages->visible() as $p): ?>
    <li>
      <a <?php e($p->isOpen(), ' class="active"') ?> href="<?php echo $p->url() ?>"><?php echo $p->title()->html() ?></a>
      <!-- don't show submenu if page is news page -->
      <?php if($p->hasVisibleChildren() && !$page->is(page('news'))): ?>
      <ul class="submenu">
        <?php foreach($p->children()->visible() as $p): ?>
        <li>
          <a href="<?php echo $p->url() ?>"><?php echo $p->title()->html() ?></a>
        </li>
        <?php endforeach ?>
      </ul>
      <?php endif ?>

    </li>
    <?php endforeach ?>
  </ul>

</nav>

As for ordering the post by date:

<?php
$posts = page('news')->children()->visible()->sortBy('publish_date', 'desc');
?>

BTW: If your posts are invisible, you can nevertheless order them in the same way.

1 Like

Hi,
Not sure where you want to display the “posts” but here’s a hint to guide you:

$posts = page('news')->children()->sortBy('date', 'desc');

This gives you a collection named posts with all the subpages of ‘news’ ordered by date.

edit: @texnixe always faster than a bullet! Thanks for the correction

1 Like

@Thiousi: there is no orderBy() method, it is called sortBy().

1 Like

@texnixe and @Thiousi Thanks to both of you.

!$page->is(page('news')) was the tidbit I was looking for. I order my stuff by naming the folders accordingly, but I keep the sorting by post date in the back of my mind for future projects. :slight_smile: