Skip to content

Instantly share code, notes, and snippets.

@hizhengfu
Created June 26, 2014 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hizhengfu/5c4c014c0ab54678a424 to your computer and use it in GitHub Desktop.
Save hizhengfu/5c4c014c0ab54678a424 to your computer and use it in GitHub Desktop.
getkirby 2 tagcloud
<?php
function tagcloud($parent, $options = array())
{
global $site;
// default values
$defaults = array(
'limit' => false,
'field' => 'tags',
'children' => 'visible',
'baseurl' => $parent->url(),
'param' => 'tag',
'sort' => 'results',
'sortdir' => 'desc'
);
// merge defaults and options
$options = array_merge($defaults, $options);
switch ($options['children']) {
case 'invisible':
$children = $parent->children()->invisible();
break;
case 'visible':
$children = $parent->children()->visible();
break;
default:
$children = $parent->children();
break;
}
$cloud = new Collection();
$ds = DIRECTORY_SEPARATOR == '/' ? ':' : ';';
foreach ($children as $p) {
$tags = str::split($p->$options['field']());
foreach ($tags as $t) {
if (is_object($cloud->get($t))) {
$cloud->get($t)->results++;
} else {
$cloud->set($t, new obj(array(
'results' => 1,
'name' => $t,
'url' => $options['baseurl'] . '/' . $options['param'] . $ds . $t,
'isActive' => (param($options['param']) == $t) ? true : false,
)));
}
}
}
$cloud->sortBy($options['sort'], $options['sortdir']);
if ($options['limit']) {
$cloud->limit($options['limit']);
}
return $cloud;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment