Display Featured article

Hey together,

currently I’m struggeling to create a featured article post for a project.
This means that the featured article is selected inside the panel and should appear on the page ‘Blog’ at the top as the first article. The presentation of the article at the top and the selection of the article with the help of the multiselect plugins works so far.

Now I would like to realize that the featured article will only be displayed once and not again between all the other articles. In addition, unfortunately I do not know how I run the pagination correct. Cause right now the featured article will be displayed on every page when I use the pagination. Maybe someone had to face the same problem…
Thanks!

<?php 
$perpage  = $page->perpage()->int();
$articles = $pages->find('blog')->children()->visible()->flip()->paginate($perpage); 
$last = $articles->last(); 
$pagination = $articles->pagination(); 
?>

<!-- Featured Article -->
<?php foreach($page->featured()->pages(',') as $f): ?>
  <div class="box">
    <h2><?= $f->title() ?></h2>
    <p><?= excerpt($f->text(), 300) ?></p>
  </div>         
<?php endforeach ?>

<!-- All Articles --> 
<?php foreach($articles as $article): // article overview ?>
  <?php if($page->featured()->pages(',') != ''): ?>
    <h2><?= $article->title() ?></h2>
    <p><?= excerpt($article->text(), 300) ?></p>
  <?php endif ?>
<?php endforeach ?>

<!-- Pagination --> 
<nav>
  <ul >
  <?php if($pagination->hasPrevPage()): ?>
  <li><a href="<?= $pagination->prevPageURL() ?>">Newer</a></li>
  <?php endif ?>
  
  <?php foreach($pagination->range(3) as $r): ?>
      <li>
        <a class="pagination-link<?php if($pagination->page() == $r) echo ' is-current' ?>" href="<?= $pagination->pageURL($r) ?>"><?= $r ?></a>
     </li>
  <?php endforeach ?>

  <?php if($pagination->hasNextPage()): ?>
  <li><a href="<?= $pagination->nextPageURL() ?>">Older</a></li>
  <?php endif ?>
  </ul>
</nav>

So, you only want to show the featured article only on the first page of the pagination? You could wrap your featured article block in an if that checks just for that:

<!-- Featured Article -->
<?php if($pagination->page() === 1): ?>
    <?php foreach($page->featured()->pages(',') as $f): ?>
      <div class="box">
        <h2><?= $f->title() ?></h2>
        <p><?= excerpt($f->text(), 300) ?></p>
      </div>         
    <?php endforeach ?>
<?php endif; ?>

And you can use not() to exclude the featured article from the collection. I’m a bit confused, are you talking about a single or multiple featured articles now?

Edit: Anyway, not() accepts a collection as argument, so this should work:

$articles = $pages->find('blog')->children()->visible(); 
$featured = $page->featured()->pages(',')
$articlesWithoutFeatured = $articles->not($featured)->flip()->paginate($perpage);

@bvdputte thanks works perfect!

@texnixe Im sorry i didnt mentioned that the featured part can contain up to 2 articles.

This code works now for me :slight_smile:

<?php 
$featured = $page->featured()->split(); 
$articles = $pages->find('blog')->children()->not($featured)->visible()->flip()->paginate($perpage);
?>
1 Like