Skip to content

Instantly share code, notes, and snippets.

@ajithrn
Created February 22, 2014 08:48
Show Gist options
  • Save ajithrn/9150621 to your computer and use it in GitHub Desktop.
Save ajithrn/9150621 to your computer and use it in GitHub Desktop.
Wordpress: breadcrumbs
<?php
/**
* only works in pages, no categories support
* */
echo '<ul>';
if ( is_page() && $post->post_parent ): // checking if the page has parent
$parent_id = $post->post_parent;
$breadcrumbs = array();
while ( $parent_id ):
$page = get_page( $parent_id );
$breadcrumbs[] = '<a href="'.get_permalink( $page->ID ).'">'.get_the_title( $page->ID ).'</a>';
$parent_id = $page->post_parent;
endwhile;
$breadcrumbs = array_reverse( $breadcrumbs );
for ( $i = 0; $i < count( $breadcrumbs ); $i++ ):
echo '<li>'.$breadcrumbs[$i].'</li>'; // print parent titles
endfor;
echo '<li>'.get_the_title().'</li>' ; // print current page title
endif;
echo '</ul>';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment