<?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>Atalaya Studio &#187; CSS tips</title>
	<atom:link href="http://atalayastudio.com/info/css-tips/feed" rel="self" type="application/rss+xml" />
	<link>http://atalayastudio.com</link>
	<description></description>
	<lastBuildDate>Mon, 08 Feb 2010 19:21:03 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Visually Controlling Menus using CSS Body Classes</title>
		<link>http://atalayastudio.com/archives/61</link>
		<comments>http://atalayastudio.com/archives/61#comments</comments>
		<pubDate>Mon, 30 Mar 2009 18:21:52 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[Themes]]></category>
		<category><![CDATA[Tips/Hints]]></category>
		<category><![CDATA[classes]]></category>
		<category><![CDATA[conditional tags]]></category>
		<category><![CDATA[CSS tips]]></category>
		<category><![CDATA[theme integration]]></category>

		<guid isPermaLink="false">http://atalayastudio.com/?p=61</guid>
		<description><![CDATA[This was written in response to a discussion on the topic of conditionally managing menus&#8230; To control the visual presence of various menu levels, a fairly simple theme modification can be made using a php function written as a WordPress action and used in conjunction with a few CSS rules.

WordPress knows which type of query [...]]]></description>
			<content:encoded><![CDATA[<p>This was written in response to a discussion on the topic of conditionally managing menus&#8230; To control the visual presence of various menu levels, a fairly simple theme modification can be made using a php function written as a <a title="WordPress Actions API" href="http://codex.wordpress.org/Plugin_API" target="_blank">WordPress action</a> and used in conjunction with a few CSS rules.</p>
<p><span id="more-61"></span></p>
<p>WordPress knows which type of query has been requested and as a result, it sets one of the is_* conditional tags. For example, is_page(), is_single(), is_category(), etc. <a title="Word Press conditional tags" href="http://codex.wordpress.org/Conditional_Tags" target="_blank">(An explanation of the topic is provided here)</a></p>
<p>If I want to modify the contents or behavior of a menu I simply add a couple of things to the theme.</p>
<p>In the theme&#8217;s functions.php file, create a function that can determine which conditional tag is active; assign a php variable to a CSS class that corresponds to the current conditional tag. Here I have named a function <strong>setBodyClass</strong></p>
<pre class="brush: php">

/**
 * This function echos a CSS class statement
* that corresponds to the current conditional tag
 */
function setBodyClass() {

    /* Home page or front page ? */
    $class = (is_home() ? &quot;homepage&quot; : is_front_page() ? &quot;frontpage&quot; : &#039;&#039;);

   /* single page ? */
   $class = (is_single() ? &quot;single-page&quot; : $class);

   /* any category ? */
   $class = (is_category() ? &quot;category&quot; : $class):

   /* A specific category ? */
   if( is_category() ) {
      $cat_id = get_category_by_slug(&quot;news-category&quot;);
      if($cat_id) {
         $class = (is_category($cat_id) ? &quot;news-category&quot;: $class);
      }
   }

   if( &#039;&#039; != $class ) {
      echo &#039; class=&quot;&#039; . $class . &#039;&quot;&#039;;
   }
}// end function

/**
 * add this action
 */
add_action(&#039;setBodyClass&#039;);
</pre>
<p>Now that we have the function written we want to use the routine in the template file where the <strong>BODY tag</strong> is created. The BODY tag can typically be found in the <strong>header.php</strong> or <strong>index.php</strong> file of your theme. After locating the BODY tag modify the tag as follows:</p>
<pre class="brush: php">
&lt;body &lt;?php do_action(&#039;setBodyClass&#039;); ?&gt; &gt;
</pre>
<h3>What have we done here?</h3>
<p>We&#8217;ve just provided a method for dynamically adding a CSS class to the body tag of the page based on a set of conditional tags. The <strong>do_action </strong>call is an <em>action hook </em>that is exclusively provided by the theme&#8217;s <strong>functions.php</strong> file. <em>Theme&#8217;s can act as their own plugin functionality provider and mine often do</em>. This technique is a preferred (<em>by me</em>) over a direct php call to the <strong>setBodyClass </strong>function. The action will add the semantic class to the BODY tag if one of the conditions I care about is true. If the none of the conditions are met, then a CSS class is not added to the BODY tag. If the <strong>setBodyClass</strong> function was <span style="text-decoration: underline;">not present</span> in the theme&#8217;s <strong>functions.php</strong> file then nothing would happen.</p>
<h3>Body Class</h3>
<p>To visually control navigation menus I can now create CSS rules that determine what the web site visitor can see when specific content is displayed on the site. This works in conjunction with the applied BODY class. For example, NAVT applies specific classes to various types of menu items. The class <strong>page_item</strong> is applied to the unordered list tag that encapsulates an anchor to a page. It also applies the class <strong>cat_item</strong> to the unordered list tag that encapsulates an anchor to a category. More specific classes are also applied by NAVT for pages, categories and other list assets that are contained in the menus. There are too many to enumerate here.</p>
<h3>Example Menu with CSS</h3>
<p>Here is a simple menu that was generated by NAVT.</p>
<pre class="brush: html">

  &lt;ul class=&#039;menu samplemenu firefox en-US&#039;&gt;
    &lt;li class=&#039;erow samplemenu_item current_page_item hometab&#039;&gt;
      &lt;a href=&#039;http://wp.local&#039; title=&#039;Home&#039; class=&#039;navt_ilink current_item&#039;&gt;Home&lt;/a&gt;
    &lt;/li&gt;
    &lt;li class=&#039;orow samplemenu_item page_item about-page pagetab&#039;&gt;
      &lt;a href=&#039;http://wp.local/?page_id=2&#039; title=&#039;About&#039; class=&#039;navt_plink&#039;&gt;About&lt;/a&gt;
    &lt;/li&gt;
    &lt;li class=&#039;orow samplemenu_item page_item local-news-cat categorytab&#039;&gt;
      &lt;a href=&#039;http://wp.local/?cat=1&#039; title=&#039;Local News&#039; class=&#039;navt_clink&#039;&gt;Local News&lt;/a&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
</pre>
<p>If, for example, I did <span style="text-decoration: underline;">not want</span> to display the &#8220;Local News&#8221; category on the homepage or the front page I can easily write a CSS rule to hide the category when the visitor is on the home page or the front page:</p>
<pre class="brush: css">
body.frontpage ul.samplemenu li.local-news-cat,
body.homepage ul.samplemenu li.local-news-cat {
    display: none;
}
</pre>
<p>The CSS rule above <span style="text-decoration: underline;">hides the display</span> of the LI tag assigned the class <strong>local-news-cat</strong> that is a descendant of the UL tag with the class <strong>samplemenu</strong> that is a descendant of the BODY tag assigned the class <strong>frontpage </strong>or <strong>homepage</strong>.</p>
<h3>Conclusion</h3>
<p>This is an easy technique to apply (<em>your milage may vary</em>). Create the menus you want with NAVT, examine the tags created by the plugin and decide what you want to control and under what conditions then write the CSS rules accordingly.</p>
<p>I hope this was helpful, have fun&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://atalayastudio.com/archives/61/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
