Advertisement
arnabkumar

Dynamic Breadcrumbs in Wordpress

Jun 8th, 2014
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.22 KB | None | 0 0
  1. Step One
  2. ================
  3.  
  4. Download/copy breadcrumbs.php data from here:
  5.  
  6.    
  7.  
  8.     <?php
  9.     function wordpress_breadcrumbs() {
  10.      
  11.       $delimiter = '&raquo;';
  12.       $name = 'Home'; //text for the 'Home' link
  13.       $currentBefore = '<span class="current">';
  14.       $currentAfter = '</span>';
  15.      
  16.       if ( !is_home() && !is_front_page() || is_paged() ) {
  17.      
  18.         echo '<div id="crumbs">';
  19.      
  20.         global $post;
  21.         $home = get_bloginfo('url');
  22.         echo '<a href="' . $home . '">' . $name . '</a> ' . $delimiter . ' ';
  23.      
  24.         if ( is_category() ) {
  25.           global $wp_query;
  26.           $cat_obj = $wp_query->get_queried_object();
  27.           $thisCat = $cat_obj->term_id;
  28.           $thisCat = get_category($thisCat);
  29.           $parentCat = get_category($thisCat->parent);
  30.           if ($thisCat->parent != 0) echo(get_category_parents($parentCat, TRUE, ' ' . $delimiter . ' '));
  31.           echo $currentBefore . 'Archive by category &#39;';
  32.           single_cat_title();
  33.           echo '&#39;' . $currentAfter;
  34.      
  35.         } elseif ( is_day() ) {
  36.           echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';
  37.           echo '<a href="' . get_month_link(get_the_time('Y'),get_the_time('m')) . '">' . get_the_time('F') . '</a> ' . $delimiter . ' ';
  38.           echo $currentBefore . get_the_time('d') . $currentAfter;
  39.      
  40.         } elseif ( is_month() ) {
  41.           echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';
  42.           echo $currentBefore . get_the_time('F') . $currentAfter;
  43.      
  44.         } elseif ( is_year() ) {
  45.           echo $currentBefore . get_the_time('Y') . $currentAfter;
  46.      
  47.         } elseif ( is_single() ) {
  48.           $cat = get_the_category(); $cat = $cat[0];
  49.           echo get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
  50.           echo $currentBefore;
  51.           the_title();
  52.           echo $currentAfter;
  53.      
  54.         } elseif ( is_page() && !$post->post_parent ) {
  55.           echo $currentBefore;
  56.           the_title();
  57.           echo $currentAfter;
  58.      
  59.         } elseif ( is_page() && $post->post_parent ) {
  60.           $parent_id  = $post->post_parent;
  61.           $breadcrumbs = array();
  62.           while ($parent_id) {
  63.             $page = get_page($parent_id);
  64.             $breadcrumbs[] = '<a href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a>';
  65.             $parent_id  = $page->post_parent;
  66.           }
  67.           $breadcrumbs = array_reverse($breadcrumbs);
  68.           foreach ($breadcrumbs as $crumb) echo $crumb . ' ' . $delimiter . ' ';
  69.           echo $currentBefore;
  70.           the_title();
  71.           echo $currentAfter;
  72.      
  73.         } elseif ( is_search() ) {
  74.           echo $currentBefore . 'Search results for &#39;' . get_search_query() . '&#39;' . $currentAfter;
  75.      
  76.         } elseif ( is_tag() ) {
  77.           echo $currentBefore . 'Posts tagged &#39;';
  78.           single_tag_title();
  79.           echo '&#39;' . $currentAfter;
  80.      
  81.         } elseif ( is_author() ) {
  82.            global $author;
  83.           $userdata = get_userdata($author);
  84.           echo $currentBefore . 'Articles posted by ' . $userdata->display_name . $currentAfter;
  85.      
  86.         } elseif ( is_404() ) {
  87.           echo $currentBefore . 'Error 404' . $currentAfter;
  88.         }
  89.      
  90.         if ( get_query_var('paged') ) {
  91.           if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ' (';
  92.           echo __('Page') . ' ' . get_query_var('paged');
  93.           if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ')';
  94.         }
  95.      
  96.         echo '</div>';
  97.      
  98.       }
  99.     }
  100.      
  101.     ?>
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112. Step Two
  113. ================
  114.  
  115. Create a folder called inc. Create a php file called breadcrumbs.php & then paste data.
  116.  
  117. Step Three
  118. ================
  119.  
  120. Call breadcrumbs.php in Theme Functions
  121.  
  122. include_once('inc/breadcrumbs.php');
  123.  
  124. Step Four
  125. ================
  126.  
  127. Use it by code below
  128.  
  129. <div class="breadcumbs">
  130.         <?php if (function_exists('wordpress_breadcrumbs')) wordpress_breadcrumbs(); ?>
  131. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement