Revision 167 is a pre-publication revision. (Viewing current revision instead.)

Breadcrumbs for Taxonomy Pages

Taken from Bill Ericson: http://www.billerickson.net/wordpress-taxonomy-breadcrumbs/ This post has been marked as old. The code might no longer work. Comments have been disabled as this is no longer maintained. Use the search on the right to find a newer tutorial. I’m working on a project right now that uses hierarchical taxonomies, and the client requested a breadcrumb along the top. I searched online but couldn’t find any good existing plugins, so thought I’d write my own. Note: This was built for WordPress 3.03. WordPress 3.1 (hopefully coming out this month) will add a lot of features to taxonomies, including hierarchical URLs. This solution is NOT designed for hierarchical URLs. If you read through the code you’ll see I’m assembling them with 3 things:
  • bloginfo('url') – This is the URL for your site
  • $item->taxonomy – This is the name of the taxonomy the term is in
  • $item->name – This is the name of the taxonomy term
Since WordPress doesn’t currently support hierarchical URLs for taxonomies, no matter how deep your term is it will still have the url structure of yoursite.com/taxonomy-name/taxonomy-term. Hopefully 3.1 will give us a URL from get_term_by(), but it doesn’t now, which is why I did it this way. With that said, here’s the code. Place this in your functions.php file (or custom_functions.php for Thesis): [php] <div id="LC1"><?php</div> <div id="LC2">/* Taxonomy Breadcrumb */</div> <div id="LC3">function be_taxonomy_breadcrumb() {</div> <div id="LC4">// Get the current term</div> <div id="LC5">$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );</div> <div id="LC6"></div> <div id="LC7">// Create a list of all the term's parents</div> <div id="LC8">$parent = $term->parent;</div> <div id="LC9">while ($parent):</div> <div id="LC10">$parents[] = $parent;</div> <div id="LC11">$new_parent = get_term_by( 'id', $parent, get_query_var( 'taxonomy' ));</div> <div id="LC12">$parent = $new_parent->parent;</div> <div id="LC13">endwhile;</div> <div id="LC14">if(!empty($parents)):</div> <div id="LC15">$parents = array_reverse($parents);</div> <div id="LC16"></div> <div id="LC17">// For each parent, create a breadcrumb item</div> <div id="LC18">foreach ($parents as $parent):</div> <div id="LC19">$item = get_term_by( 'id', $parent, get_query_var( 'taxonomy' ));</div> <div id="LC20">$url = get_bloginfo('url').'/'.$item->taxonomy.'/'.$item->slug;</div> <div id="LC21">echo '<li><a href="'.$url.'">'.$item->name.'</a></li>';</div> <div id="LC22">endforeach;</div> <div id="LC23">endif;</div> <div id="LC24"></div> <div id="LC25">// Display the current term in the breadcrumb</div> <div id="LC26">echo '<li>'.$term->name.'</li>';</div> <div id="LC27">}</div> [/php] To use it in your theme, simply call be_taxonomy_breadcrumb() within a <ul>

Revisions

  • April 29, 2012 @ 14:25:33 [Current Revision] by PeterLugg
  • April 29, 2012 @ 14:21:23 by PeterLugg

Revision Differences

April 29, 2012 @ 14:21:23Current Revision
Content
 Added: Taken from Bill Ericson: http: //www.billerickson.net/wordpress- taxonomy-breadcrumbs/
 Added: This post has been marked as <strong>old</strong>. The code might no longer work. Comments have been disabled as this is no longer maintained. Use the search on the right to find a newer tutorial.
 Added: I’m working on a project right now that uses hierarchical taxonomies, and the client requested a breadcrumb along the top. I searched online but couldn’t find any good existing plugins, so thought I’d write my own.
 Added: Note: This was built for WordPress 3.03. WordPress 3.1 (hopefully coming out this month) will add a lot of features to taxonomies, including hierarchical URLs. This solution is NOT designed for hierarchical URLs. If you read through the code you’ll see I’m assembling them with 3 things:
Deleted: Added: <ul>
 Added: <li><code>bloginfo( 'url')</code> – This is the URL for your site</li>
 Added: <li><code>$item- &gt;taxonomy</code> – This is the name of the taxonomy the term is in</li>
 Added: <li><code>$item- &gt;name</code> – This is the name of the taxonomy term</li>
 Added: </ul>
 Added: Since WordPress doesn’t currently support hierarchical URLs for taxonomies, no matter how deep your term is it will still have the url structure of yoursite.com/ taxonomy-name/ taxonomy-term. Hopefully 3.1 will give us a URL from <code>get_ term_by()</code>, but it doesn’t now, which is why I did it this way.
 Added: With that said, here’s the code. Place this in your functions.php file (or custom_functions.php for Thesis):
 Added: [php]
 Added: &lt;div id=&quot;LC1&quot; &gt;&lt;?php&lt;/div&gt;
 Added: &lt;div id=&quot;LC2&quot;&gt;/* Taxonomy Breadcrumb */&lt;/div&gt;
 Added: &lt;div id=&quot;LC3&quot; &gt;function be_taxonomy_breadcrumb() {&lt;/div&gt;
 Added: &lt;div id=&quot;LC4&quot;&gt;// Get the current term&lt;/div&gt;
 Added: &lt;div id=&quot;LC5&quot;&gt;$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );&lt;/div&gt;
 Added: &lt;div id=&quot;LC6&quot; &gt;&lt;/div&gt;
 Added: &lt;div id=&quot;LC7&quot;&gt;// Create a list of all the term's parents&lt;/div&gt;
 Added: &lt;div id=&quot;LC8&quot;&gt;$parent = $term-&gt;parent; &lt;/div&gt;
 Added: &lt;div id=&quot;LC9&quot;&gt;while ($parent):&lt;/div&gt;
 Added: &lt;div id=&quot;LC10&quot; &gt;$parents[] = $parent;&lt;/div&gt;
 Added: &lt;div id=&quot;LC11&quot; &gt;$new_parent = get_term_by( 'id', $parent, get_query_var( 'taxonomy' ));&lt;/div&gt;
 Added: &lt;div id=&quot;LC12&quot;&gt;$parent = $new_parent-&gt; parent;&lt;/div&gt;
 Added: &lt;div id=&quot;LC13&quot; &gt;endwhile; &lt;/div&gt;
 Added: &lt;div id=&quot;LC14&quot; &gt;if(!empty($parents)): &lt;/div&gt;
 Added: &lt;div id=&quot;LC15&quot; &gt;$parents = array_reverse( $parents);&lt;/div&gt;
 Added: &lt;div id=&quot;LC16&quot; &gt;&lt;/div&gt;
 Added: &lt;div id=&quot;LC17&quot;&gt;// For each parent, create a breadcrumb item&lt;/div&gt;
 Added: &lt;div id=&quot;LC18&quot;&gt;foreach ($parents as $parent):&lt;/div&gt;
 Added: &lt;div id=&quot;LC19&quot;&gt;$item = get_term_by( 'id', $parent, get_query_var( 'taxonomy' ));&lt;/div&gt;
 Added: &lt;div id=&quot;LC20&quot;&gt;$url = get_bloginfo( 'url').'/'.$item- &gt;taxonomy.'/ '.$item-&gt;slug; &lt;/div&gt;
 Added: &lt;div id=&quot;LC21&quot;&gt;echo '&lt;li&gt;&lt;a href=&quot;'.$url.'&quot; &gt;'.$item-&gt;name.'&lt; /a&gt;&lt;/li&gt; ';&lt;/div&gt;
 Added: &lt;div id=&quot;LC22&quot; &gt;endforeach; &lt;/div&gt;
 Added: &lt;div id=&quot;LC23&quot; &gt;endif;&lt;/div&gt;
 Added: &lt;div id=&quot;LC24&quot; &gt;&lt;/div&gt;
 Added: &lt;div id=&quot;LC25&quot;&gt;// Display the current term in the breadcrumb&lt;/div&gt;
 Added: &lt;div id=&quot;LC26&quot;&gt;echo '&lt;li&gt;'.$term- &gt;name.'&lt; /li&gt;';&lt;/div&gt;
 Added: &lt;div id=&quot;LC27&quot; &gt;}&lt;/div&gt;
 Added: [/php]
 Added: To use it in your theme, simply call <code>be_ taxonomy_breadcrumb( )</code> within a <code>&lt; ul&gt;</code>

Note: Spaces may be added to comparison text to allow better line wrapping.

Tags: ,

No comments yet.

Leave a Reply