Snippet · Mecha CMS

Code snippet collection for developers.

Automatic Keyword Links

Author: Taufik Nurrohman · 386 Views

Tweet

Automatically generate list of keyword links based on the current page slug.

This snippet will prepend a list of keyword links on top of your page content, just like page tags but for the search:

Hook::set('page.content', function($content, $lot) use($language, $site) {
    if (!isset($lot['path']) || strpos($lot['path'], PAGE) !== 0) {
        return $content;
    }
    $page = Page::open($lot['path'])->get([
        'slug' => null,
        'url' => null
    ]);
    $a = [];
    if (!empty($page['slug'])) {
        $url = $site->is === 'page' ? Path::D($page['url']) : $page['url'];
        foreach (explode('-', $page['slug']) as $v) {
            $a[] = HTML::a($v, $url . HTTP::query([$site->q => $v]), false, ['rel' => 'tag']); 
        }
    }
    sort($a);
    $s  = '<p><strong>' . $language->{count($ss) === 1 ? 'keyword' : 'keywords'} . ':</strong>';
    $s .= ' ' . implode(', ', $a) . '</p>';
    return $s . $content;
}, 2.1);

0 Comments

No comments yet.