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

WordPress – Custom Taxonomy Breadcrumbs

Taken from: http://www.free-php.net/780/wordpress-custom-taxonomy-breadcrumbs/ While developing a theme that uses custom taxonomies and custom post types, I ran across the need to include breadcrumb navigation to the custom taxonomies. After a couple of days of searching around and not finding any real solutions, I ended up creating my own function to do it. Simply copy this to your functions.php file: [php] <?php function postTypeCrumbs($postType, $postTax) { $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); $taxonomy = get_taxonomy($term->taxonomy); $parents = get_ancestors( $term->term_id, $postTax ); $parents = array_reverse($parents); $archive_link = get_post_type_archive_link( $postType ); echo '<ul class="ax_crumbs">'; if ($taxonomy) { echo '<li><a href="' . $archive_link . '" title="' . $taxonomy->labels->name . '">' . $taxonomy->labels->name . '</a> &raquo; </li>'; } foreach ( $parents as $parent ) { $p = get_term( $parent, $postTax ); echo '<li><a href="' . get_term_link($p->slug, $postTax) . '" title="' . $p->name . '">' . $p->name . '</a> <span>&raquo;</span> </li>'; } if ($term) { echo '<li>' . $term->name . '</li>'; } echo '</ul>'; } ?> [/php] Paste this where ever you would like the taxonomy to display. [php] <?php postTypeCrumbs('post type', 'taxonomy name'); ?> [/php]

Revisions

  • April 29, 2012 @ 14:28:18 [Current Revision] by PeterLugg
  • April 29, 2012 @ 14:26:24 by PeterLugg

Revision Differences

April 29, 2012 @ 14:26:24Current Revision
Content
 Added: Taken from: http:/ /www.free-php.net/ 780/wordpress- custom-taxonomy- breadcrumbs/
 Added: While developing a theme that uses custom taxonomies and custom post types, I ran across the need to include breadcrumb navigation to the custom taxonomies. After a couple of days of searching around and not finding any real solutions, I ended up creating my own function to do it.
 Added: Simply copy this to your functions.php file:
 Added: [php]
 Added: &lt;?php
 Added: function postTypeCrumbs($postType, $postTax) {
 Added: $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
 Added: $taxonomy = get_taxonomy( $term-&gt;taxonomy);
 Added: $parents = get_ancestors( $term-&gt;term_id, $postTax );
 Added: $parents = array_reverse($parents);
 Added: $archive_link = get_post_type_ archive_link( $postType );
 Added: echo '&lt;ul class=&quot;ax_ crumbs&quot;&gt;';
 Added: if ($taxonomy) {
 Added: echo '&lt;li&gt;&lt;a href=&quot;' . $archive_link . '&quot; title=&quot;' . $taxonomy-&gt; labels-&gt;name . '&quot;&gt;' . $taxonomy-&gt; labels-&gt;name . '&lt;/a&gt; &amp;raquo; &lt;/li&gt;';
 Added: }
 Added: foreach ( $parents as $parent ) {
 Added: $p = get_term( $parent, $postTax );
 Added: echo '&lt;li&gt;&lt;a href=&quot;' . get_term_link( $p-&gt;slug, $postTax) . '&quot; title=&quot;' . $p-&gt;name . '&quot;&gt;' . $p-&gt;name . '&lt;/a&gt; &lt;span&gt;&amp; raquo;&lt;/span&gt; &lt;/li&gt;';
 Added: }
 Added: if ($term) {
 Added: echo '&lt;li&gt;' . $term-&gt;name . '&lt;/li&gt;';
 Added: }
 Added: echo '&lt;/ul&gt;';
Deleted: Added: }
 Added: ?&gt;
 Added: [/php]
 Added: Paste this where ever you would like the taxonomy to display.
 Added: [php]
 Added: &lt;?php postTypeCrumbs('post type', 'taxonomy name'); ?&gt;
 Added: [/php]

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

Tags: ,

No comments yet.

Leave a Reply