Tags

    Code snippet to work with tags.

    Predefined variables for tags:

    By default, the database of tags is alphanumeric sorted.

    List all tags

    <?php
        // Returns an array with all the tags
        $items = getTags();
    
        foreach ($items as $tag) {
            // Each tag is an Tag-Object
            echo 'Tag name: '       . $tag->name();
            echo 'Tag key: '        . $tag->key();
            echo 'Tag link: '       . $tag->permalink();
            echo 'Tag number of pages: '    . count($tag->pages());
        }
    ?>

    Alternative:

    <?php
        foreach ($tags->keys() as $key) {
            // Create Tag-Object
            $tag = new Tag($key);
    
            echo 'Tag name: '       . $tag->name();
            echo 'Tag key: '        . $tag->key();
            echo 'Tag link: '       . $tag->permalink();
            echo 'Tag number of pages: '    . count($tag->pages());
        }
    ?>

    List tags that have pages

    <?php
        $items = getTags();
    
        foreach ($items as $tag) {
            // Each tag is an Tag-Object
            if (count($tag->pages())>0) {
                echo 'Tag name: '   . $tag->name();
                echo 'Tag key: '    . $tag->key();
                echo 'Tag link: '   . $tag->permalink();
            }
        }
    ?>

    List all tags and the pages related to the tag

    <?php
        $items = getTags();
    
        foreach ($items as $tag) {
            // Each tag is an Tag-Object
            echo 'Tag name: ' . $tag->name();
    
            // The method $tag->pages() returns all the pages keys releated to the tag
            foreach ($tag->pages() as $pageKey) {
                $page = new Page($pageKey);
                echo '- Page title: ' . $page->title();
            }
        }
    ?>
    <?php
            // Tag key
            $tagKey = 'example';
    
        // The tag is an Tag-Object
            $tag = getTag($tagKey);
    
            // Print the tag name
            echo 'Tag name: ' . $tag->name();
    
            // Print the pages title related to the tag "example"
            foreach ($tag->pages() as $pageKey) {
            $page = new Page($pageKey);
            echo $page->title();
            }
    ?>

    Get the active tag

    <?php
        // Check if the user is browsing a tag
        if ($WHERE_AM_I=='tag') {
            // Get the tag key from the URL
            $tagKey = $Url->slug();
    
            // Create the Tag-Object
            $tag = new Tag($tagKey);
    
            // Print the tag name
            echo $tag->name();
        }
    ?>

    Print the tags of a page:

    <?php
        $returnsArray = true;
    
        $items = $page->tags($returnsArray);
    
        foreach ($items as $tagKey=>$tagName) {
            echo $tagName;
        }
    ?>

    Print the tags and permalink:

    <?php
        $returnsArray = true;
    
        $items = $page->tags($returnsArray);
    
        foreach ($items as $tagKey=>$tagName) {
            $tag = new Tag($tagKey);
    
            echo '<a href="'.$tag->permalink().'">'.$tag->name().'</a>;
        }
    ?>
    Collaborate with us and edit this page