{"id":101,"date":"2012-06-21T07:56:54","date_gmt":"2012-06-21T06:56:54","guid":{"rendered":"http:\/\/jonas.kihlsten.se\/blog\/?p=101"},"modified":"2012-06-21T07:57:51","modified_gmt":"2012-06-21T06:57:51","slug":"clean-wordpress-tag-cloud","status":"publish","type":"post","link":"http:\/\/jonas.kihlsten.se\/blog\/2012\/06\/clean-wordpress-tag-cloud\/","title":{"rendered":"Clean WordPress tag cloud"},"content":{"rendered":"<p>The default WordPress tag cloud includes nasty stuff like inline styling. I needed to be able to style the tags with a single class that included the tag weight in its name. It also needed the tags to be displayed in a list. The result turned the separate tag markup from<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;a href=&quot;http:\/\/randomsite.com\/tag\/lolcats\/&quot; class=&quot;tag-link-5&quot; title=&quot;3 tag&quot; style=&quot;font-size: 32pt&quot;&gt;Lolcats&lt;\/a&gt;\r\n<\/pre>\n<p>into<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;li class=&quot;tag-10&quot;&gt;\r\n\t&lt;a href=&quot;http:\/\/randomsite.com\/tag\/lolcats\/&quot;&gt;Lolcats&lt;\/a&gt;\r\n&lt;\/li&gt;\r\n<\/pre>\n<p>First we create a simple value object representing a tag.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nclass Custom_Tag\r\n{\r\n\tvar $name, $link, $class;\r\n\t\r\n\tpublic function Custom_Tag($name, $link, $class)\r\n\t{\r\n\t\t$this-&gt;name = $name;\r\n\t\t$this-&gt;link = $link;\r\n\t\t$this-&gt;class = $class;\r\n\t}\r\n}\r\n<\/pre>\n<p>Then we go through all the tags and assign each a class depending on its weight. Those occurring the least will get the class tag-1 and those occurring most frequent will get the class tag-10. The function will return an array of Custom_Tag objects.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nfunction custom_generate_tag_cloud($tags)\r\n{\r\n\tif($tags) {\r\n\t\t$smallest_class = 1;\r\n\t\t$largets_class = 10;\r\n\t\t\r\n\t\t$counts = array();\r\n\t\tforeach ($tags as $key =&gt; $tag) {\r\n\t\t\t$counts&#x5B;$key] = $tag-&gt;count;\r\n\t\t}\r\n\t\t\r\n\t\t$min_count = min($counts);\r\n\t\t$spread = max($counts) - $min_count;\r\n\t\tif ( $spread &lt;= 0 )\r\n\t\t\t$spread = 1;\r\n\t\t\r\n\t\t$class_spread = $largets_class - $smallest_class;\r\n\t        if ( $class_spread &lt; 0 )\r\n\t                $class_spread = 1;\r\n\t        $class_step = $class_spread \/ $spread;\r\n\t\t\r\n\t\t$cloud_tags = array();\r\n\t\tforeach ($tags as $key =&gt; $tag) {\r\n\t\t\t$count = $counts&#x5B;$key];\r\n\t\t\t$class = 'tag-' . round($smallest_class + ( ( $count - $min_count ) * $class_step ));\r\n\t\t\t$cloud_tags&#x5B;] = new Custom_Tag($tag-&gt;name, $tag-&gt;link, $class);\r\n\t\t}\r\n\t}\r\n\t\r\n\treturn $cloud_tags;\r\n}\r\n<\/pre>\n<p>Finally we collect the tag objects created in the function above and output some html. We also add a filter hook for &#8220;wp_generate_tag_cloud&#8221;.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nfunction custom_tag_cloud($return, $tags, $args)\r\n{\r\n\t$tag_array = custom_generate_tag_cloud($tags);\r\n\t$cloud_tags = array();\r\n\t\r\n\t$return = &quot;&lt;ul&gt;\\n&quot;;\r\n\tforeach ($tag_array as $tag) {\r\n\t\t$cloud_tags&#x5B;] = '&lt;li class=&quot;' . $tag-&gt;class . '&quot;&gt;&lt;a href=&quot;' . $tag-&gt;link . '&quot;&gt;' . $tag-&gt;name . &quot;&lt;\/a&gt;&lt;\/li&gt;\\n&quot;;\r\n\t}\r\n\t$return .= join('', $cloud_tags);\r\n\t$return .= &quot;&lt;\/ul&gt;\\n&quot;;\r\n\t\r\n\treturn $return;\r\n}\r\n\r\nadd_filter('wp_generate_tag_cloud', 'custom_tag_cloud', 10, 3);\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The default WordPress tag cloud includes nasty stuff like inline styling. I needed to be able to style the tags with a single class that included the tag weight in its name. It also needed the tags to be displayed in a list. The result turned the separate tag markup from &lt;a href=&quot;http:\/\/randomsite.com\/tag\/lolcats\/&quot; class=&quot;tag-link-5&quot; title=&quot;3 &hellip; <a href=\"http:\/\/jonas.kihlsten.se\/blog\/2012\/06\/clean-wordpress-tag-cloud\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Clean WordPress tag cloud<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[18,30],"class_list":["post-101","post","type-post","status-publish","format-standard","hentry","category-wordpress","tag-tag-cloud","tag-wordpress"],"_links":{"self":[{"href":"http:\/\/jonas.kihlsten.se\/blog\/wp-json\/wp\/v2\/posts\/101","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/jonas.kihlsten.se\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/jonas.kihlsten.se\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/jonas.kihlsten.se\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/jonas.kihlsten.se\/blog\/wp-json\/wp\/v2\/comments?post=101"}],"version-history":[{"count":22,"href":"http:\/\/jonas.kihlsten.se\/blog\/wp-json\/wp\/v2\/posts\/101\/revisions"}],"predecessor-version":[{"id":124,"href":"http:\/\/jonas.kihlsten.se\/blog\/wp-json\/wp\/v2\/posts\/101\/revisions\/124"}],"wp:attachment":[{"href":"http:\/\/jonas.kihlsten.se\/blog\/wp-json\/wp\/v2\/media?parent=101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/jonas.kihlsten.se\/blog\/wp-json\/wp\/v2\/categories?post=101"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/jonas.kihlsten.se\/blog\/wp-json\/wp\/v2\/tags?post=101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}