Get all tags of a site

Post Reply
User avatar
CrazyBread
Master Bludit
Posts: 73
Joined: Tue Jan 19, 2016 9:51 pm
Location: Germany
Has thanked: 4 times
Been thanked: 5 times
Contact:

I'm looking for a possibility to get all tags of my site for a tag-cloud on my index-page.

Is there a way to call a certain plugin (in this case plugin "tags") or get them by a certain class?
User avatar
CrazyBread
Master Bludit
Posts: 73
Joined: Tue Jan 19, 2016 9:51 pm
Location: Germany
Has thanked: 4 times
Been thanked: 5 times
Contact:

Okay, I found a way, but maybe there's a better one:

Code: Select all

<div class="tagcloud">
	<?php
		//Build Tag URL
		$uritag = get_defined_vars()['Site']->uriFilters('tag');
		//Both domain base and uritag have a slash, so cut one off
		$url = substr(DOMAIN_BASE, 0, -1).$uritag;
		//Get all tags
		$dbTags = get_defined_vars()['dbTags'];
		$tags = array_keys($dbTags->getField('postsIndex'));
		sort($tags);
		foreach ($tags as $tag)
		{
			echo '<div class="singletag">';
			echo '<a href="'.$url.$tag.'">'.$tag.'</a>';
			echo '</div>'.PHP_EOL;
		}
	?>
</div>
User avatar
Edi
Site Admin
Posts: 3121
Joined: Sun Aug 09, 2015 5:01 pm
Location: Zurich
Has thanked: 54 times
Been thanked: 77 times
Contact:

You can also use functions from the plugin. For example:

Code: Select all

<?php

		global $dbTags;
		global $Url;

		$db = $dbTags->db['postsIndex'];
		$filter = $Url->filters('tag');

		$tagArray = array();

		foreach($db as $tagKey=>$fields)
		{
			$tagArray[] = array('tagKey'=>$tagKey, 'count'=>$dbTags->countPostsByTag($tagKey), 'name'=>$fields['name']);
		}

		usort($tagArray, function($a, $b) {
			return $b['count'] - $a['count'];
		});

		foreach($tagArray as $tagKey=>$fields)
		{
			// Print the parent
			echo '<li><a href="'.HTML_PATH_ROOT.$filter.'/'.$fields['tagKey'].'">'.$fields['name'].' ('.$fields['count'].')</a></li>';
		}

?>
User avatar
CrazyBread
Master Bludit
Posts: 73
Joined: Tue Jan 19, 2016 9:51 pm
Location: Germany
Has thanked: 4 times
Been thanked: 5 times
Contact:

Thank you!
This is the more smarter way.
komersreal
Jr. Bludit
Posts: 1
Joined: Fri Feb 17, 2017 12:45 pm

Edi wrote:You can also use functions from the plugin. For example:

Code: Select all

<?php

		global $dbTags;
		global $Url;

		$db = $dbTags->db['postsIndex'];
		$filter = $Url->filters('tag');

		$tagArray = array();

		foreach($db as $tagKey=>$fields)
		{
			$tagArray[] = array('tagKey'=>$tagKey, 'count'=>$dbTags->countPostsByTag($tagKey), 'name'=>$fields['name']);
		}

		usort($tagArray, function($a, $b) {
			return $b['count'] - $a['count'];
		});

		foreach($tagArray as $tagKey=>$fields)
		{
			// Print the parent
			echo '<li><a href="'.HTML_PATH_ROOT.$filter.'/'.$fields['tagKey'].'">'.$fields['name'].' ('.$fields['count'].')</a></li>';
		}

?>
Better version of the tag cloud pluggin:

Code: Select all

<?php

class pluginTags extends Plugin {

	public function init()
	{
		$this->dbFields = array(
			'label'=>'Tags',
			'sort'=>'date'
		);
	}

	public function form()
	{
		global $Language;

		$html  = '<div>';
		$html .= '<label>'.$Language->get('Plugin label').'</label>';
		$html .= '<input name="label" id="jslabel" type="text" value="'.$this->getDbField('label').'">';
		$html .= '</div>';

		$html .= '<div>';
		$html .= $Language->get('Sort the tag list by').': <select name="sort">';

		foreach(array('alpha' => 'Alphabetical order',
		              'count' => 'Number of times each tag has been used',
		              'date'  => 'Date each tag was first used') as $key=>$value) {
			if ($key == $this->getDbField('sort')) {
				$html .= '<option value="'.$key.'" selected>'.$Language->get($value).'</option>';
			} else {
				$html .= '<option value="'.$key.'">'.$Language->get($value).'</option>';
			}
		}
		$html .= '</select>';
		$html .= '</div>';

		return $html;
	}

	public function siteSidebar()
	{
		global $Language;
		global $dbTags;
		global $Url;

		$db = $dbTags->db['postsIndex'];
		$filter = $Url->filters('tag');

		$html  = '<div class="plugin plugin-tags">';
		$html .= '<h2>'.$this->getDbField('label').'</h2>';
		$html .= '<div class="plugin-content">';
		//$html .= '<ul>';

		$tagArray = array();
		$tagKvantoMaks = 0;

		foreach($db as $tagKey=>$fields)
		{
			$tagArray[] = array('tagKey'=>$tagKey, 'count'=>$dbTags->countPostsByTag($tagKey), 'name'=>$fields['name']);
			
			if($dbTags->countPostsByTag($tagKey) > $tagKvantoMaks)
				$tagKvantoMaks = $dbTags->countPostsByTag($tagKey);
		}

		// Sort the array based on options
		if ($this->getDbField('sort') == "count")
		{
			usort($tagArray, function($a, $b) {
				return $b['count'] - $a['count'];
			});
		}
		elseif ($this->getDbField('sort') == "alpha")
		{
			usort($tagArray, function($a, $b) {
				return strcmp($a['tagKey'], $b['tagKey']);
			});
		}


		$tiparoMezuro = 10;
		
		foreach($tagArray as $tagKey=>$fields)
		{
			if($tagKvantoMaks > 1)
				$tiparoMezuro = 10 + (20-10) * (($fields['count']-1)/($tagKvantoMaks-1));
			else
				$tiparoMezuro = 10;
			
			// Print the parent
			//$html .= '<li><a href="'.HTML_PATH_ROOT.$filter.'/'.$fields['tagKey'].'">'.$fields['name'].' ('.$fields['count'].')</a></li>';
			$html .= '<a style="font-size: '.$tiparoMezuro.'pt;" href="'.HTML_PATH_ROOT.$filter.'/'.$fields['tagKey'].'">'.$fields['name'].'('.$fields['count'].')</a> ';
		}
		//$html .= '</ul>';
 		$html .= '</div>';
 		$html .= '</div>';

		return $html;
	}
}
Post Reply