<?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>Banhawi &#187; Usability</title>
	<atom:link href="http://banhawi.com/category/usability/feed/" rel="self" type="application/rss+xml" />
	<link>http://banhawi.com</link>
	<description>Web Design and Development News and Tutorials</description>
	<lastBuildDate>Thu, 29 Jul 2010 00:50:25 +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>Wordpress Conditional Tags In Action (Breadcrumbs)</title>
		<link>http://banhawi.com/2009/06/wordpress-conditional-tags-in-action-breadcrumbs/</link>
		<comments>http://banhawi.com/2009/06/wordpress-conditional-tags-in-action-breadcrumbs/#comments</comments>
		<pubDate>Tue, 30 Jun 2009 22:03:56 +0000</pubDate>
		<dc:creator>Banhawi</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Usability]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[Breadcrumbs]]></category>

		<guid isPermaLink="false">http://banhawi.com/?p=82</guid>
		<description><![CDATA[Wordpress is the most powerful blogging system ever , even you can use it to do any type of sites (E-Commerce , CMS, Galleries .. etc) i think wordpress is going forward to be a framework , all this because of the powerful functions it offers to developers .
I&#8217;m going to use some of the [...]]]></description>
			<content:encoded><![CDATA[<p>Wordpress is the most powerful blogging system ever , even you can use it to do any type of sites (E-Commerce , CMS, Galleries .. etc) i think wordpress is going forward to be a framework , all this because of the powerful functions it offers to developers .</p>
<p>I&#8217;m going to use some of the wordpress Conditional Tags plus other wordpress functions to create a Breadcrumbs Navigation for wordpress .</p>
<p>Read More about Breadcrumbs and why to use them &#8220;<a href="http://psychology.wichita.edu/surl/usabilitynews/52/breadcrumb.htm" target="_blank">Breadcrumb Navigation: Further Investigation of Usage</a>&#8221;</p>
<p style="text-align: center;"><img class="size-full wp-image-90 aligncenter" title="breadcrumbs Navigation" src="http://banhawi.com/wp-content/uploads/2009/06/breadcrumbs.jpg" alt="breadcrumbs Navigation" width="552" height="245" /></p>
<div class="demo"><a href='http://banhawi.com/wp-content/uploads/2009/06/breadcrumbs.zip'>Source</a></div>
<p><span id="more-82"></span></p>
<h3>Step One</h3>
<p>Create a new file within your theme folder and call it breadcrumbs.php or whatever you want</p>
<h3>Step Two</h3>
<p>In home page we don&#8217;t have to show the user the breadcrumbs navigation so we will do this</p>
<pre class="brush: php;">
&lt;?php
	if ( is_home() ){}
	else {
		echo &quot;&lt;div class=\&quot;breadcrumbs\&quot;&gt;&lt;ul class=\&quot;breadcrumbs\&quot;&gt; &quot;;
		echo &quot;&lt;/ul&gt;&lt;/div&gt;&quot;;
	}
?&gt;
</pre>
<p>The code above is pretty simple , we used our first Conditional Tag ( is_home() ) within an if &#8211; else statement.</p>
<p>is_home() used to check whether the current page is the homepage or not .</p>
<p>We used it here to do nothing when it&#8217;s homepage and applying a simple html codes when it&#8217;s not homepage .</p>
<h3>Step Three</h3>
<p>Let&#8217;s Add some css styles .</p>
<pre class="brush: css;">
ul.breadcrumbs {
	list-style: none;
	padding: 0;
	margin: 0;
	float: left;
}
ul.breadcrumbs li {
	float: left;
	margin: 0 5px 0 0;
	padding: 0;
}
ul.breadcrumbs li:before {
	content: &quot;\00BB \0020&quot;;
}
</pre>
<p>We simply styled the ul.breadcrumbs menu as a horizontal menu and added right angle quote before every &#8220;li&#8221; member .</p>
<h3>Step Four</h3>
<p>The blog name must be added before any thing so we&#8217;ve to add it to our code .</p>
<pre class="brush: php;">
&lt;?php
	if ( is_home() ){}
	else {
		echo &quot;&lt;div class=\&quot;breadcrumbs\&quot;&gt;&lt;ul class=\&quot;breadcrumbs\&quot;&gt; &quot;;
		echo &quot;&lt;li&gt;&lt;a href=\&quot; &quot;.get_settings('home').&quot; \&quot;&gt;&quot;;
		echo  bloginfo('name').&quot;&lt;/a&gt;&lt;/li&gt;&quot;;
		echo &quot;&lt;/ul&gt;&lt;/div&gt;&quot;;
	}
?&gt;</pre>
<p>In the code above we&#8217;ve added the blog name using the wordpress function bloginfo(&#8217;name&#8217;); within &#8220;li&#8221; tags and linked it to the blog url using the wordpress function get_settings(&#8217;home&#8217;) which retrieves the blog link .</p>
<h3>Step Five</h3>
<p><strong>Output</strong> :   » Blog Name » Category Name  »</p>
<p><strong>Another Output </strong>:   » Blog Name » Category Name  » Sub-Category Name »</p>
<p>We will start our breadcrumbs code in the Category Pages</p>
<pre class="brush: php;">
if ( is_category() ) {
	$cate = single_cat_title(&quot;&quot;,false);
	$cat = get_cat_ID($cate);
	echo &quot;&lt;li&gt;&quot;.(get_category_parents($cat, TRUE,&quot; &amp;raquo; &quot;)).&quot;&lt;/li&gt;&quot;;
}
</pre>
<p>In the code above we used is_category() &#8211; Conditional Tag , which checks if the current page is a category one or not , then the code above will only be executed within the category pages .</p>
<p>We can simply print the current category using single_cat_title(); but we won&#8217;t as we can have multi-leveled categories so we&#8217;ve to show the current category plus it&#8217;s parents if there any .</p>
<p>We can get the parents of any category using the get_category_parents(); function , and we will use it , but the problem is the output of the single_cat_title(); is the category name and to get the category parents using the get_category_parents(); function we have to pass it the category ID , so we will use get_cat_ID($cate); function to get the current category ID from the current category name .</p>
<p><em>Note :</em> single_cat_title(&#8221;",false); function&#8217;s second argument must be false as we don&#8217;t want the function to print the category name by itself we want to use the output within another function without printing it so u&#8217;ve to declare the second argument to false .</p>
<h3>Step Six</h3>
<p>Let&#8217;s apply our breadcrumbs to archives , search , 404 error pages .</p>
<p><strong>Output 1</strong>:   » Blog Name » Archives</p>
<p><strong>Output 2</strong>:   » Blog Name » Search Results</p>
<p><strong>Output 3</strong>:   » Blog Name » 404 Not Found</p>
<pre class="brush: php;">
elseif ( is_archive() &amp;&amp; !is_category() ) {
	echo &quot;&lt;li&gt;Archives&lt;/li&gt;&quot;;
}
elseif ( is_search() ) {
	echo &quot;&lt;li&gt;Search Results&lt;/li&gt;&quot;;
}
elseif ( is_404() ) {
	echo &quot;&lt;li&gt;404 Not Found&lt;/li&gt;&quot;;
}
</pre>
<p>Obviously you know what&#8217;s the function of the above code , it&#8217;s pretty easy code .</p>
<p>We first used is_archive() &#8211; Conditional Tag to check if the page is a  archive pages and printing &#8220;Archives&#8221; if it&#8217;s  an Archive page , we also used<br />
&amp;&amp; !is_category() &#8211; because category page are also archive pages and we don&#8217;t want to print Archives within the category pages so we added &amp;&amp; (AND) !(NOT) is_category()</p>
<p>Then we used is_search() &#8211; Conditional Tag to check if the page is a search page and printing &#8220;Search Results&#8221; if it&#8217;s a search page .</p>
<p>Then we used is_404() &#8211; Conditional Tag to check is the page is a 404 error page and printing &#8220;404 Not Found&#8221; if it&#8217;s a 404 error page .</p>
<h3>Step Seven</h3>
<p>Let&#8217;s apply the breadcrumbs to the single post pages</p>
<p><strong>Output</strong> :   » Blog Name » Category Name  » Post Name</p>
<pre class="brush: php;">
elseif ( is_single() ) {
	$category = get_the_category();
	$category_id = get_cat_ID($category[0]-&gt;cat_name);
	$cat_link = get_category_link( $category_id );
	echo &quot;&lt;li&gt;&lt;a href=\&quot; &quot;. $cat_link .&quot; \&quot;&gt;&quot; . $category[0]-&gt;cat_name.&quot;&lt;/a&gt;&lt;/li&gt;&quot;;
	echo &quot;&lt;li&gt;&quot;;
	echo the_title().&quot;&lt;/li&gt;&quot;;
}
</pre>
<p>In the code above we used is_single() &#8211; Conditional Tag to check if the page is a single post page and printing the category followed by the post name if it&#8217;s a single post page.</p>
<p><strong>$category = get_the_category(); </strong>To get the category .<br />
<strong>$category_id = get_cat_ID($category[0]-&gt;cat_name);</strong> To get the category ID from the category name ($category[0]-&gt;cat_name) , $category[0] that ZERO because we will only get the first category from the categories array (posts may be posted in many categories and we only need  the first) .<br />
<strong>$cat_link = get_category_link( $category_id );</strong> To get the category URL .</p>
<p>then we just print the category , followed by the post title using the_title()- wordpress function .</p>
<h3>Step Eight</h3>
<p>Let&#8217;s apply the breadcrumbs to the Pages</p>
<p><strong>Output</strong> :   » Blog Name » Page Name</p>
<p><strong>Another Output</strong> :   » Blog Name » Page Name » Sub-Page Name</p>
<pre class="brush: php;">
			elseif ( is_page() ) {

				if ($post-&gt;post_parent) {

  					$children = $post-&gt;post_title;

  					$parent = get_page($post-&gt;post_parent);

  					echo &quot;&lt;li&gt;&quot;.$parent-&gt;post_title.&quot;&lt;/li&gt;&quot;.&quot;&lt;li&gt;&quot;.$children.&quot;&lt;/li&gt;&quot;;
				}
				elseif ( $post-&gt;post_parent == 0 ) {

					echo &quot;&lt;li&gt;&quot;;

					echo the_title().&quot;&lt;/li&gt;&quot;;

				}
			}
</pre>
<p>This is the hard part , as there&#8217;s a wordpress function to retrieve page parents . So we wrote some code to retrieve the first page , this will give you the ability to show your breadcrumbs navigation in the second level pages but not in the third level pages as it&#8217;ll retrieve the second level page parent only without the first level page parent .</p>
<p>We start our code block with is_page() &#8211; Conditional Tag to check if it&#8217;s a normal page .</p>
<p>After that we write another if &#8211; else statement to check if the current page has a parent and if not we will just print the current page title .</p>
<p>If the current page has a parent :</p>
<p>We store the current page name in the $children variable  ( $children = $post-&gt;post_title; ) .<br />
Then we use the get_page(); wordpress function to get the parent page and store it in the $parent varialble $parent = get_page($post-&gt;post_parent);  .</p>
<p>Finally we print the page parent name followed by the page name as shown in line 10 .</p>
<pre class="brush: php;">
				elseif ( $post-&gt;post_parent == 0 ) {

					echo &quot;&lt;li&gt;&quot;;

					echo the_title().&quot;&lt;/li&gt;&quot;;

				}
</pre>
<p>The code above means if there&#8217;s no page parent just print the title of the page .</p>
<h2>The Complete Code </h2>
<pre class="brush: php;">
	&lt;?php
	/*
		Wordpress Breadcrumbs code by Banhawi.com

	*/

		if ( is_home() ){}

		else {

			echo &quot;&lt;div class=\&quot;breadcrumbs\&quot;&gt;&lt;ul class=\&quot;breadcrumbs\&quot;&gt; &quot;;

			echo &quot;&lt;li&gt;&lt;a href=\&quot; &quot;.get_settings('home').&quot; \&quot;&gt;&quot;;
			echo  bloginfo('name').&quot;&lt;/a&gt;&lt;/li&gt;&quot;;

			if ( is_category() ) {
				$cate = single_cat_title(&quot;&quot;,false);
				$cat = get_cat_ID($cate);
				echo &quot;&lt;li&gt;&quot;.(get_category_parents($cat, TRUE,&quot; &amp;raquo; &quot;)).&quot;&lt;/li&gt;&quot;;
			}
			elseif ( is_archive() &amp;&amp; !is_category() ) {
				echo &quot;&lt;li&gt;Archives&lt;/li&gt;&quot;;
			}
			elseif ( is_search() ) {
				echo &quot;&lt;li&gt;Search Results&lt;/li&gt;&quot;;
			}
			elseif ( is_404() ) {
				echo &quot;&lt;li&gt;404 Not Found&lt;/li&gt;&quot;;
			}
			elseif ( is_single() ) {
				$category = get_the_category();
				$category_id = get_cat_ID($category[0]-&gt;cat_name);
				$cat_link = get_category_link( $category_id );
				echo &quot;&lt;li&gt;&quot;.(get_category_parents($category, TRUE,&quot;&quot;)).&quot;&lt;/li&gt;&quot;;
				echo &quot;&lt;li&gt;&lt;a href=\&quot; &quot;. $cat_link .&quot; \&quot;&gt;&quot; . $category[0]-&gt;cat_name.&quot;&lt;/a&gt;&lt;/li&gt;&quot;;
				echo &quot;&lt;li&gt;&quot;;
				echo the_title().&quot;&lt;/li&gt;&quot;;
			}
			elseif ( is_page() ) {

				if ($post-&gt;post_parent) {

  					$children = $post-&gt;post_title;

  					$parent = get_page($post-&gt;post_parent);

  					echo &quot;&lt;li&gt;&quot;.$parent-&gt;post_title.&quot;&lt;/li&gt;&quot;.&quot;&lt;li&gt;&quot;.$children.&quot;&lt;/li&gt;&quot;;
				}
				elseif ( $post-&gt;post_parent == 0 ) {

					echo &quot;&lt;li&gt;&quot;;

					echo the_title().&quot;&lt;/li&gt;&quot;;

				}
			}

			echo &quot;&lt;/ul&gt;&lt;/div&gt;&quot;;
		}
	?&gt;
</pre>
<p>For search result page , you can display the search query instead of &#8220;Search Results&#8221; just use this code </p>
<pre class="brush: php;">
			elseif ( is_search() ) {
				echo &quot;&lt;li&gt;&quot;.the_search_query().&quot;&lt;/li&gt;&quot;;
			}
</pre>
<h3>Finally</h3>
<p>Just use the include function to include the breadcrumbs script any where in your theme .</p>
<pre class="brush: php;">
	&lt;?php include (TEMPLATEPATH . '/breadcrumbs.php'); ?&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://banhawi.com/2009/06/wordpress-conditional-tags-in-action-breadcrumbs/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
	</channel>
</rss>
