Created
June 17, 2014 21:29
-
-
Save abhilash0001/3066847c86cb03115652 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/********************************************* | |
Breadcrumbs for wordpress without any | |
plugin. This also works for custom post | |
types. | |
You can refactor to make it more efficient. :) | |
Note: For your custom post types look at | |
line 47 | |
**********************************************/ | |
function get_breadcrumbs() | |
{ | |
global $wp_query; | |
if ( !is_home() ){ | |
// Start the UL | |
echo '<ul>'; | |
// Add the Home link | |
echo '<li><a href="'. get_settings('home') .'">Home</a></li>'; | |
if ( is_category() ) | |
{ | |
$catTitle = single_cat_title( "", false ); | |
$cat = get_cat_ID( $catTitle ); | |
echo "<li> ". get_category_parents( $cat, TRUE, " " ) ."</li>"; | |
} | |
elseif ( is_archive() && !is_category() ) | |
{ | |
echo "<li> Archives</li>"; | |
} | |
elseif ( is_search() ) { | |
echo "<li> Search Results</li>"; | |
} | |
elseif ( is_404() ) | |
{ | |
echo "<li> 404 Not Found</li>"; | |
} | |
elseif ( is_single() ) | |
{ | |
// Custom post types (single) breadcrumbs | |
$path=$_SERVER['REQUEST_URI']; | |
$pathItems = explode("/", $path); | |
if ( is_singular( 'event' ) | |
|| is_singular( 'story' ) | |
|| is_singular( 'career' ) | |
|| is_singular( 'case-studies' ) ) { | |
foreach ($pathItems as $value) { | |
if ($value != "" && $value != "/"){ | |
$url .= "/" . $value; | |
echo "<li><a href='" . $url . "'>" . str_replace('-', ' ', $value) . "</a></li>"; | |
} | |
} | |
} | |
else { | |
$category = get_the_category(); | |
$category_id = get_cat_ID( $category[0]->cat_name ); | |
echo '<li> '. get_category_parents( $category_id, TRUE, " " ); | |
echo the_title('','', FALSE) ."</li>"; | |
} | |
} | |
elseif ( is_page() ) | |
{ | |
$post = $wp_query->get_queried_object(); | |
if ( $post->post_parent == 0 ){ | |
echo "<li> ".the_title('','', FALSE)."</li>"; | |
} else { | |
$title = the_title('','', FALSE); | |
$ancestors = array_reverse( get_post_ancestors( $post->ID ) ); | |
array_push($ancestors, $post->ID); | |
foreach ( $ancestors as $ancestor ){ | |
if( $ancestor != end($ancestors) ){ | |
echo '<li> <a href="'. get_permalink($ancestor) .'">'. strip_tags( apply_filters( 'single_post_title', get_the_title( $ancestor ) ) ) .'</a></li>'; | |
} else { | |
echo '<li> '. strip_tags( apply_filters( 'single_post_title', get_the_title( $ancestor ) ) ) .'</li>'; | |
} | |
} | |
} | |
} | |
// End the UL | |
echo "</ul>"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment