Mecha CMS

Mecha CMS blog and documentation.

Conditional Excerpt

Updated: Sunday, 07 August 2016

Conditionally display the article excerpt if available.

Conditionally display the article excerpt if available. Otherwise, fallback to the article description.

<!-- index.php -->
<div class="post-body">
  <?php if($article->excerpt): ?>
    <?php echo $article->excerpt; ?>
    <?php if(strpos($article->excerpt, '<a class="fi-link" ') === false): ?>
      <p><a href="<?php echo $article->url . '#' . sprintf($config->excerpt->id, $article->id); ?>"><?php echo $speak->read_more; ?></a></p>
    <?php endif; ?>
  <?php else: ?>
    <p><?php echo Text::parse($article->description, '->text'); ?></p>
  <?php endif; ?>
</div>

In version 1.2.x you can simply load the built-in chunk to the articles loop:

…
<article class="post post-index" id="post-<?php echo $article->id; ?>">
  <h4 class="post-title">
    <?php if($article->link): ?>
    <a href="<?php echo $article->link; ?>">
    <?php else: ?>
    <a href="<?php echo $article->url; ?>">
    <?php endif; ?>
      <?php echo $article->title; ?>
    </a>
  </h4>
  <!-- lot/extends/chunk/article.body.index.php -->
  <?php Shield::chunk('article.body.index'); ?>
</article>
…
Donation and Email Subscription