Mecha CMS

Code snippet collection for developers.

Basic Tags Widget

Author: Taufik Nurrohman · 105 Views

Tweet

List all of the available tags from the current page folder.

Table of Content

Requires tags extension.

Tag List 1 

<?php

$_base = 'article';

echo '<h3>Tags</h3>';
if ($_kinds = glob(PAGE . DS . $_base . DS . '*' . DS . 'kind.data', GLOB_NOSORT)) {
    echo '<ul>';
    $_data = [];
    foreach ($_kinds as $_kind) {
        $_kind = e(file_get_contents($_kind));
        if (!is_array($_kind)) {
            continue;
        }
        foreach ($_kind as $_k) {
            $_data[] = $_k;
        }
    }
    $_slug = Extend::state('tag', 'path', 'tag');
    foreach (array_count_values($_data) as $_k => $_v) {
        if (($_slug = To::tag($_k)) === false) continue;
        $_tag = new Page(TAG . DS . $_slug . '.page', [], 'tag');
        $_u = $url . '/' . To::url($_base) . '/' . $_slug;
        echo '<li>' . HTML::a($_tag->title, $_u . '/' . $_tag->slug) . ' (' . $_v . ')</li>';
    }
    echo '</ul>';
} else {
    echo '<p>No tags yet.</p>';
}

?>
  • $_base → Select from which page folder the tags you want to list (relative to lot\page folder).

Tag List 2 

<?php

$_path = $url->path;

if ($site->tag) { // → `blog/tag/tag-slug`
    $_path = URL::I($url->path); // Remove page offset in URL path
    $_path = Path::D($_path, 2);
} else if ($site->is === 'page') { // → `blog/page-slug`
    $_path = Path::D($_path);
}

echo '<h3>Tags</h3>';
if ($_kinds = glob(PAGE . DS . $_path . DS . '*' . DS . 'kind.data', GLOB_NOSORT)) {
    echo '<ul>';
    $_data = [];
    foreach ($_kinds as $_kind) {
        $_kind = e(file_get_contents($_kind));
        if (!is_array($_kind)) {
            continue;
        }
        foreach ($_kind as $_k) {
            $_data[] = $_k;
        }
    }
    foreach (array_count_values($_data) as $_k => $_v) {
        if (($_slug = To::tag($_k)) === false) {
            continue;
        }
        $_tag = new Page(TAG . DS . $_slug . '.page', [], 'tag');
        echo '<li>' . HTML::a($_tag->title, $_tag->url) . ' (' . $_v . ')</li>';
    }
    echo '</ul>';
} else {
    echo '<p>No tags yet.</p>';
}

?>

0 Comments

No comments yet.