<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Maff Wilson&#039;s Webby Home &#187; top category level</title>
	<atom:link href="http://maffwilson.co.uk/tag/top-category-level/feed" rel="self" type="application/rss+xml" />
	<link>http://maffwilson.co.uk</link>
	<description>Projects, notes, play and contact details.</description>
	<lastBuildDate>Wed, 28 Jul 2010 17:25:03 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>[Solved] Wordpress Top Category Function</title>
		<link>http://maffwilson.co.uk/74-solved-wordpress-top-category-function</link>
		<comments>http://maffwilson.co.uk/74-solved-wordpress-top-category-function#comments</comments>
		<pubDate>Fri, 22 Jan 2010 12:24:28 +0000</pubDate>
		<dc:creator>Maff</dc:creator>
				<category><![CDATA[Notes]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[styling]]></category>
		<category><![CDATA[top category]]></category>
		<category><![CDATA[top category level]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://maffwilson.co.uk/?p=74</guid>
		<description><![CDATA[For the new geeks.co.uk design I needed a way of finding which top level category any given post was in and use that in the CSS to make various design elements of the page the specific colour of that top level category. 
On geeks.co.uk, some posts are in a top level category, eg all News [...]]]></description>
			<content:encoded><![CDATA[<p>For the new <a href="http://geeks.co.uk">geeks.co.uk</a> design I needed a way of finding which top level category any given post was in and use that in the CSS to make various design elements of the page the specific colour of that top level category. </p>
<p>On geeks.co.uk, some posts are in a top level category, eg all News articles, and some are in sub categories, eg some Game Reviews are in Wii but not PSP etc.</p>
<p>Also, CSS won&#8217;t work with spaces in the names of labels so for categories like &#8220;Game Reviews&#8221; we have to remove the space to be useful as a CSS label.</p>
<p>Sometimes you want the function call to echo the category name to the page, eg when it&#8217;s used as a title of the page or in breadcrumbs.  Sometimes you just want it returned to use in a CSS label, for example.</p>
<p>So here is my function:</p>
<p><code>function get_top_cat($css = false, $echo = false){<br />
	$maffcat = (array) get_the_category();<br />
	$maffpar = get_cat_name($maffcat[0]->category_parent);<br />
	if ($maffpar == ''){<br />
		$maffcat = (array) get_the_category();<br />
		$maffpar = $maffcat[0]->cat_name;<br />
	}</p>
<p>	if ($css){$maffpar = str_replace (" ", "", $maffpar);}</p>
<p>	if ($echo){ echo $maffpar;}<br />
	else{<br />
	return $maffpar;<br />
	}</p>
<p>}</code></p>
<p>Stick that in your theme&#8217;s functions.php and you should be able to call it by, for example,</p>
<p><code>get_top_cat('', 'true') - will put top category name with spaces on your page<br />
get_top_cat('true', '') - will put top category name without spaces in your code</code></p>
<p>in your webpage files like single.php, archive.php etc</p>
<p>Nb. this works for two-level categories &#8211; it seems that this could easily be extended for more levels by further nesting.</p>
]]></content:encoded>
			<wfw:commentRss>http://maffwilson.co.uk/74-solved-wordpress-top-category-function/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
