Advertisement
keha76

WordPress Breadcrumbs

May 25th, 2013
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.13 KB | None | 0 0
  1. /**
  2.  * Breadcrumbs
  3.  *
  4.  * Prints a list with breadcrumbs.
  5.  *
  6.  * @param array $attributes shortcode parameters
  7.  * @return html
  8.  * @author keha76
  9.  */
  10. function keha76_breadcrumbs( $attributes = array() ) {
  11.    
  12.     // Access Globals
  13.     global $author,$post, $wp_query;
  14.    
  15.     // Extract shortcode attributes
  16.     extract( shortcode_atts( array(
  17.         'container_class' => 'breadcrumbs',
  18.         'container_id'    => '',
  19.         'current_before'  => "\n\t<li class=\"breadcrumbs-current\">",
  20.         'current_after'   => '</li>',
  21.         'items_wrap'      => '<ul id="%1$s" class="%2$s">%3$s'."\n".'</ul>'
  22.     ), $attributes ) );
  23.    
  24.     // Variables
  25.     $html                 = '';
  26.     $home_text            = apply_filters( 'keha76_breadcrumbs_home_text', __( 'Home', 'keha76' ) );
  27.     $home_url             = home_url();
  28.     $posts_page_title     = get_the_title( get_option( 'page_for_posts', true ) );
  29.     $posts_page_permalink = get_permalink( get_option( 'page_for_posts', true ) );
  30.    
  31.     // Conditional Terms
  32.     if ( is_home() && !is_front_page() && !is_paged() ) {
  33.        
  34.         // We are on the static posts page.
  35.         $html .= "\n\t<li><a href=\"" . $home_url . "\">" . $home_text . "</a></li>";
  36.         $html .= "\n\t<li class=\"breadcrumbs-current\">". $posts_page_title . "</li>";
  37.        
  38.     } elseif ( is_front_page() || is_home() ) {
  39.        
  40.         if ( is_paged() ) {
  41.             // We are on the front home page.
  42.             $html .= "\n\t<li><a href=\"" . $home_url . "\">" . $home_text . "</a></li>";
  43.         } else {
  44.             // We are on the front home page.
  45.             $html .= "\n\t<li>" . $home_text . "</li>";
  46.         }
  47.        
  48.        
  49.     } elseif ( !is_home() && !is_front_page() || is_paged() ) {
  50.        
  51.         // Current page is paged and not front page or home.
  52.         $html .= "\n\t<li><a href=\"" . $home_url . "\">" . $home_text . "</a></li>";
  53.        
  54.         if ( is_category() ) {
  55.            
  56.             $category = get_category( get_query_var( 'cat' ), false );
  57.            
  58.             if ( $category->parent != 0 ) {
  59.                 $category_parents = get_category_parents( $category->parent, true, ',' );
  60.                 $categories = split(',', $category_parents );
  61.                
  62.                 foreach ( $categories as $cat ) {
  63.                     if ( $cat != '' ) {
  64.                         $html .= $current_before;
  65.                         $html .= $cat;
  66.                         $html .= $current_after;
  67.                     }
  68.                 }
  69.             }
  70.            
  71.             $html .= $current_before;
  72.             $html .= single_cat_title( '', false );
  73.             $html .= $current_after;
  74.            
  75.         } elseif ( is_tax() ) {
  76.            
  77.             $parent = get_term_by( 'id', $wp_query->queried_object->term_id, $wp_query->queried_object->taxonomy );
  78.            
  79.             if ( $parent->parent != 0 ) {
  80.                
  81.                 while ( $parent->parent != 0 ) {
  82.                     $term_id = $parent->parent;
  83.                     $parent = get_term_by( 'id', $term_id, $wp_query->queried_object->taxonomy );
  84.                     $link = get_term_link( $parent );
  85.                     $output[] = "\n\t".'<li><a href="' . $link . '">' . $parent->name .'</a></li>';
  86.                 }
  87.                
  88.                 $output_reverse = array_reverse( $output );
  89.                 foreach ( $output_reverse as $list_item ) {
  90.                     $html .= $list_item;
  91.                 }
  92.             }
  93.            
  94.             $html .= "\n\t".'<li>' . $wp_query->queried_object->name .'</li>';
  95.        
  96.         } elseif ( is_day() ) {
  97.            
  98.             $html .= "\n\t".'<li><a href="' . get_year_link( get_the_time( 'Y' ) ) . '">' . get_the_time( 'Y' ) . '</a></li>';
  99.             $html .= "\n\t".'<li><a href="' . get_month_link( get_the_time( 'Y' ), get_the_time( 'm' ) ) . '">' . get_the_time( 'F' ) . '</a></li>';
  100.            
  101.             $html .= $current_before;
  102.             get_the_time( 'd' );
  103.             $html .= $current_after;
  104.            
  105.         } elseif ( is_month() ) {
  106.            
  107.             $html .= "\n\t".'<li><a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a></li>';
  108.            
  109.             $html .= $current_before;
  110.             get_the_time( 'F' );
  111.             $html .= $current_after;
  112.  
  113.         } elseif ( is_year() ) {
  114.            
  115.             $html .= $current_before;
  116.             get_the_time( 'Y' );
  117.             $html .= $current_after;
  118.  
  119.         } elseif ( is_single() && !is_attachment() ) {
  120.                        
  121.             $html .= $current_before;
  122.             $html .= get_the_title();
  123.             $html .= $current_after;
  124.            
  125.         } elseif ( is_attachment() ) {
  126.            
  127.             $post_parent = get_post( $post->post_parent );
  128.             $post_title = trim( $parent->post_title );
  129.            
  130.             $html .= "\n\t".'<li><a href="' . get_permalink( $post_parent ) . '">' . $post_title . '</a></li>';
  131.            
  132.             $html .= $current_before;
  133.             get_the_time( 'd' );
  134.             $html .= $current_after;
  135.            
  136.         } elseif ( is_page() && !$post->post_parent ) {
  137.            
  138.             $html .= $current_before;
  139.             $html .= get_the_title();
  140.             $html .= $current_after;
  141.            
  142.         } elseif ( is_page() && $post->post_parent ) {
  143.            
  144.             $post_parent = $post->post_parent;
  145.             $post_childs = array();
  146.            
  147.             while ( $post_parent ) {
  148.                 $page = get_page( $post_parent );
  149.                 $post_childs[] = "\n\t".'<li><a href="' . get_permalink( $page->ID) . '">' . get_the_title( $page->ID ) . '</a></li>';
  150.                 $post_parent = $page->post_parent;
  151.             }
  152.            
  153.             $post_childs = array_reverse( $post_childs );
  154.            
  155.             foreach ( $post_childs as $post_child ) {
  156.                 $html .= $post_child;
  157.             }
  158.            
  159.             $html .= $current_before;
  160.             $html .= get_the_title();
  161.             $html .= $current_after;
  162.        
  163.         } elseif ( is_search() ) {
  164.            
  165.             $search_text = __( 'Search results for <strong>%1$s</strong>', 'keha76-blank' );
  166.            
  167.             $html .= $current_before;
  168.             $html .= printf( $search_text, get_search_query() );
  169.             $html .= $current_after;
  170.            
  171.         } elseif ( is_tag() ) {
  172.            
  173.             $html .= $current_before;
  174.             $html .= single_tag_title( '', false );
  175.             $html .= $current_after;
  176.            
  177.         } elseif ( is_author() ) {
  178.            
  179.             $userdata = get_userdata( $author );
  180.            
  181.             $html .= $current_before;
  182.             $html .= __( 'Error 404', 'keha76-blank' );
  183.             $html .= $current_after;
  184.            
  185.         } elseif ( is_404() ) {
  186.                        
  187.             $html .= $current_before;
  188.             $html .= __( 'Error 404', 'keha76-blank' );
  189.             $html .= $current_after;
  190.            
  191.         }
  192.        
  193.     }
  194.    
  195.     // If current page is paged
  196.     if ( get_query_var( 'paged' ) ) {
  197.        
  198.         if ( is_home() ) {
  199.             $html .= "\n\t<li class=\"breadcrumbs-current\"><a href=\"$posts_page_permalink\">". $posts_page_title . "</a></li>";
  200.         }
  201.  
  202.         $html .= "\n\t<li class=\"breadcrumbs-paged\">";
  203.         $html .= 'Page ' . get_query_var( 'paged' ) . ' of ' . $wp_query->max_num_pages;
  204.         $html .= $current_after;
  205.  
  206.     }
  207.    
  208.     $output = sprintf( $items_wrap, $container_id, $container_class, $html );
  209.     return $output;
  210. }
  211.  
  212. add_shortcode( 'breadcrumbs', 'keha76_breadcrumbs' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement