Snippet · Mecha CMS

Code snippet collection for developers.

Manual Post Excerpt

Author: Taufik Nurrohman · 435 Views

Tweet

Add manual post excerpt feature.

Table of Content

Hook::set('page.excerpt', function($content, $lot) {
    $s = Page::open($lot['path'])->get([
        'description' => null,
        'content' => null
    ]);
    // Excerpt cropper does not exist, return the page description!
    // If page description is empty, return a fake excerpt generated by the page content!
    if (!$s['content'] || strpos($s['content'], '<!-- cut -->') === false) {
        return $s['description'] ?: To::snippet($s['content']);
    }
    // Return the first part!
    return trim(explode('<!-- cut -->', $s['content'])[0]);
});

Usage 

How to enable the manual post excerpt:

---
title: Post Title
description: Post description.
author: Taufik Nurrohman
type: Markdown
...

This paragraph is going to be the post excerpt.

<!-- cut -->

Rest of the post content goes here.

To show the manual post excerpt in your shield:

<?php echo $page->excerpt; ?>

Info: The plugin version of this snippet is now available as excerpt plugin.

0 Comments

No comments yet.