<?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>Interactive IQ : The Hessler Design Blog &#187; WordPress</title>
	<atom:link href="http://www.hesslerdesign.com/blog/category/wordpress/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.hesslerdesign.com/blog</link>
	<description>Smart Developers = Smart Websites</description>
	<lastBuildDate>Thu, 26 Jan 2012 23:00:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Making Your WordPress Menu Items Smarter</title>
		<link>http://www.hesslerdesign.com/blog/tutorials/making-your-wordpress-menu-items-smarter/</link>
		<comments>http://www.hesslerdesign.com/blog/tutorials/making-your-wordpress-menu-items-smarter/#comments</comments>
		<pubDate>Wed, 16 Dec 2009 19:20:55 +0000</pubDate>
		<dc:creator>Anthony</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[themes]]></category>

		<guid isPermaLink="false">http://www.hesslerdesign.com/blog/?p=270</guid>
		<description><![CDATA[In my current work with designing my custom WordPress theme for this blog, I came up with a few nifty little tricks to make the theme smarter. One particular thing I found tricky was how to make the top nav (or any nav, for that matter) menu items smart enough to be classified as &#8220;active&#8221; [...]]]></description>
			<content:encoded><![CDATA[<p>In my current work with designing my custom WordPress theme for this blog, I came up with a few nifty little tricks to make the theme smarter. One particular thing I found tricky was how to make the top nav (or any nav, for that matter) menu items smart enough to be classified as &#8220;active&#8221; if the user was on a page in a given section. My answer came in a nice little solution called Page Templates, and a built-in function called <a title="WordPress Codex - Conditional Tags" href="http://codex.wordpress.org/Conditional_Tags#Is_a_Page_Template" target="_blank"><code>is_page_template()</code></a>.</p>
<h4>Step 1: Create a New Page Template</h4>
<p>If you&#8217;re familiar with WordPress, you may know that you can create Page Templates that differ from the &#8220;index.php&#8221; page template. All you need to do when creating a new Page Template is save your new file as a unique file name (with a few exceptions) in your Theme directory, and put the following code at the top of the file:</p>
<div style="margin-left:24px; color:#f90;"><code>&lt;?php<br />
/*<br />
Template Name: My Cool Template<br />
*/<br />
?&gt;</code></div>
<p>This little snippet of code defines your file as &#8220;My Cool Template&#8221;, or whatever template name you give it. There&#8217;s plenty more about making Page Templates at the <a title="WordPress Codex - Creating Your Own Page Templates" href="http://codex.wordpress.org/Pages#Creating_Your_Own_Page_Templates" target="_blank">WordPress Codex, in the Pages section</a>.</p>
<h4>Step 2: Assign a Page Template to a Page</h4>
<p>Once you&#8217;ve got your page templates set up, you can assign a specific page template to one of your pages in the &#8216;Pages&#8217; section of your WordPress Admin. All that you need to do is get into &#8216;Edit&#8217; mode for a page, then in the &#8216;Attributes&#8217; section on the right, select your template under the &#8216;Template&#8217; section. It&#8217;s that easy. (If you want more info on different page attributes that can be set, check out <a title="WordPress Support - Page Attributes" href="http://en.support.wordpress.com/pages/page-attributes/" target="_blank">WordPress Support for Page Attributes</a>.)</p>
<h4>Step 3: Update the Menu List Items</h4>
<p>Once the page templates have been set up, it&#8217;s time to set up the top nav list items to be smart enough to be classified  as &#8220;active&#8221; if the page being viewed is an instance of that page template.</p>
<p>(<em>Note: When I was creating my custom template, I hard-coded my nav links because I knew exactly what links I wanted to have, and because I wasn&#8217;t interested in my theme being shared and used by others. With that said, the logic below may need to be edited if used for a template whose navigation is built dynamically.</em>)</p>
<p>In the code where the navigation items are built, I initially had the following syntax used for each menu item:</p>
<div style="margin-left:24px; margin-bottom:18px;"><code><span style="color: #000099;">&lt;li&gt;</span><span style="color: #006600;">&lt;a href=</span><span style="color: #0000ff;">"</span><span style="color: #ff0000;">&lt;?php</span> bloginfo<span style="color: #000099;">(</span><span style="color: #cc0000;">'url'</span><span style="color: #000099;">)</span>; <span style="color: #ff0000;">?&gt;</span><span style="color: #0000ff;">/mysection/"</span><span style="color: #006600;">&gt;</span>My Section<span style="color: #006600;">&lt;/a&gt;</span><span style="color: #000099;">&lt;/li&gt;</span></code></div>
<p>This is your standard menu list item syntax for a menu item that&#8217;s hard-coded. It&#8217;s smart to use the <a title="WordPress Codex - Blog Info" href="http://codex.wordpress.org/Template_Tags/bloginfo#Parameters" target="_blank"><code>bloginfo('url')</code></a> function inside of the <code>href</code> to get the URL of your blog, and then append any additional directories to the end (if needed). This way, if you switch URLs, or do end up sharing the theme with others, it won&#8217;t be hard-coded to just one URL. But each <code>&lt;li&gt;</code> tag needs to have the ability to be appended with <code>class="active"</code> if the page being viewed is on a page with a given page template. For that, we need to add in another function (is_page_template()), and wrap it inside of a conditional statement:</p>
<div style="margin-left:24px; margin-bottom:18px;"><code><span style="color: #000099;">&lt;li</span> <span style="color: #ff0000;">&lt;?php</span> <span style="color: #006600;">if</span><span style="color: #000099;">(</span>is_page_template<span style="color: #000099;">(</span><span style="color: #cc0000;">'my_cool_template.php'</span><span style="color: #000099;">))</span> <span style="color: #0000ff;">:</span> <span style="color: #ff0000;">?&gt;</span> <span style="color: #000099;">class=</span><span style="color: #0000ff;">"active"</span> <span style="color: #ff0000;">&lt;?php</span> <span style="color: #006600;">endif</span>; <span style="color: #ff0000;">?&gt;</span>&gt;<span style="color: #006600;">&lt;a href=</span>"<span style="color: #ff0000;">&lt;?php</span> bloginfo<span style="color: #000099;">(</span><span style="color: #cc0000;">'url'</span><span style="color: #000099;">)</span>; <span style="color: #ff0000;">?&gt;</span><span style="color: #0000ff;">/mysection/"</span><span style="color: #006600;">&gt;</span>My Section<span style="color: #006600;">&lt;/a&gt;</span><span style="color: #000099;">&lt;/li&gt;</span></code></div>
<div style="margin-bottom:6px;">In the code above, we added the PHP logic:</div>
<div style="margin-left:24px; margin-top:0px; margin-bottom:12px;"><code><span style="color: #ff0000;">&lt;?php</span> <span style="color: #006600;">if</span> <span style="color: #000099;">(</span>is_page_template<span style="color: #000099;">(</span><span style="color: #cc0000;">'my_cool_template.php'</span><span style="color: #000099;">))</span> <span style="color: #0000ff;">:</span> <span style="color: #ff0000;">?&gt;</span> <span style="color: #ff0000;">&lt;?php</span> <span style="color: #006600;">endif</span>; <span style="color: #ff0000;">?&gt;</span></code></div>
<div style="margin-bottom:6px;">And then, between the blocks of PHP code, we add the &#8216;active&#8217; class to assign to the list item:</div>
<div style="margin-left:24px; margin-bottom:18px; margin-top:0px;"><code><span style="color: #000099;">class=</span><span style="color: #0000ff;">"active"</span></code></div>
<p>The <a title="WordPress Codex - Conditional Tags" href="http://codex.wordpress.org/Conditional_Tags#Is_a_Page_Template" target="_blank"><code>is_page_template()</code></a> function checks the page against the file name of the template passed into it. When used inside a conditional statement, it will return a <code>true</code> or <code>false</code> value, depending on whether or not the page being viewed uses that template. And after applying this &#8220;active&#8221; logic to our list items, and as long as the CSS file has a style for linked list items classified as active (i.e. <code>li.active a</code>), any list item whose conditional statement returns a true will have the &#8216;active&#8217; class assigned to it.</p>
<h4>That&#8217;s a Wrap</h4>
<p>So there you have it. A simple bit of conditional logic (assuming you&#8217;re decently familiar with PHP) that you can put into your menu list items so the links can be classified according to the page being viewed. Until next time, happy coding!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hesslerdesign.com/blog/tutorials/making-your-wordpress-menu-items-smarter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

