RtThemesSupport

rt-breadcrumb.php

May 16th, 2014
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.89 KB | None | 0 0
  1. <?php
  2. #-----------------------------------------
  3. #   RT-Theme rt_breadcrumb.php
  4. #-----------------------------------------
  5.  
  6. add_action("rt_breadcrumb_menu", "rt_breadcrumb", 10, 1 );
  7.  
  8. /* RT-Breadcrumb Function */
  9. if( ! function_exists('rt_breadcrumb') ){
  10.     function rt_breadcrumb( $args = array() ){
  11.         global $rt_taxonomy, $post, $rt_delimiter, $rt_opened_divs, $wp_query;
  12.  
  13.         //the breadcrumb text before the menu
  14.         $breadcrumb_text = get_option(RT_THEMESLUG.'_breadcrumb_text') ? get_option(RT_THEMESLUG.'_breadcrumb_text')."&nbsp;" : "";
  15.  
  16.         //default values
  17.         $defaults =  array(
  18.             'rt_delimiter'   => ' <span class="icon-angle-right"></span> ',
  19.             'wrap_before' => '',
  20.             'wrap_after'  => '',
  21.             'breadcrumb_text' => ''.rt_wpml_t(RT_THEMESLUG, 'Breadcrumb Menu Text', $breadcrumb_text ).'',
  22.             'home'        => __( 'Home', 'rt_theme' ),
  23.         );
  24.  
  25.         $args = wp_parse_args( $args, $defaults );
  26.  
  27.         //extract variables
  28.         extract( $args );
  29.  
  30.         //define variables
  31.         $rt_is_bbpress = $breadcrumb = "";
  32.      
  33.         /*  WooCommerce Breadcrumb */
  34.         if ( function_exists( 'woocommerce_breadcrumb' ) ) {
  35.             if( is_woocommerce() ){
  36.                 $defaults = array(
  37.                     'delimiter'  => $rt_delimiter,
  38.                     'wrap_before'  => $wrap_before . '<div itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a class="icon-home" itemprop="url" href="'. RT_BLOGURL .'"><span title="'. get_bloginfo('name') .'">'.$home.'</span></a></div>' .$rt_delimiter,
  39.                     'wrap_after' => $wrap_after,
  40.                     'before'   => '',
  41.                     'after'   => '',
  42.                     'home'    => false,
  43.                 );
  44.        
  45.                 woocommerce_breadcrumb($defaults);
  46.                 return false;
  47.             }
  48.         }
  49.  
  50.         /*  YOAST Breadcrumb */
  51.         $yoast_enabled=false;
  52.         if ( function_exists( 'yoast_breadcrumb' ) ) {
  53.             $yoast_options = get_option( 'wpseo_internallinks' );
  54.              if(isset($yoast_options['breadcrumbs-enable'])  && !empty($yoast_options['breadcrumbs-enable'])){
  55.                 yoast_breadcrumb($wrap_before ,$wrap_after);
  56.                 return false;
  57.         }
  58.         }
  59.        
  60.         $breadcrumb .= $wrap_before;
  61.         $breadcrumb .= ! empty( $breadcrumb_text ) ? $breadcrumb_text." " : "";
  62.  
  63.         /*
  64.           BBPRESS Breadcrumb
  65.           use BBpress Breadcrumb if for bbpress pages
  66.         */
  67.         if ( function_exists( 'bbp_breadcrumb' ) ) {
  68.             global $rt_bb_breadcrumb, $rt_wrap_before,$rt_wrap_after,$rt_breadcrumb_text;
  69.  
  70.             $rt_wrap_before = $wrap_before;
  71.             $rt_wrap_after = $wrap_after;
  72.             $rt_breadcrumb_text = $breadcrumb_text;
  73.  
  74.             //check the current page is if it is part of bbpress
  75.             if ( is_bbpress() ){  
  76.                 $breadcrumb .= $rt_bb_breadcrumb;
  77.                 $rt_is_bbpress = true;
  78.             }  
  79.         }
  80.  
  81.         //if its not a bbpress page run default breadcrumb
  82.         if( ! $rt_is_bbpress ){
  83.  
  84.                 //Home Page
  85.                     if( ! is_home() && ! is_front_page() ){
  86.                         $breadcrumb .= rt_create_breadcrumb_item( array( "class" => "icon-home", "sublink" => false, "url"=> RT_BLOGURL, "title"=> get_bloginfo('name'), "link_name" => $home ) );
  87.                     }
  88.  
  89.                 //Pages
  90.                 if ( is_page() && ! is_home() && ! is_front_page() ){
  91.                     $breadcrumb .= rt_page_parents();
  92.                 }
  93.  
  94.                 //Single posts
  95.                 elseif ( is_single() && !is_attachment() ){
  96.  
  97.                
  98.                     // Get post type
  99.                     $post_type = get_post_type();
  100.  
  101.                     //regular single posts
  102.                     if($post_type == 'post'){
  103.  
  104.                         //get base page
  105.                         $breadcrumb .= rt_get_base_page("blog");
  106.  
  107.                         $this_category = get_the_category();  
  108.  
  109.                         if( isset( $this_category[0] ) ){
  110.                             $breadcrumb .= rt_get_category_parents( $this_category[0] );
  111.                         }
  112.  
  113.                         $breadcrumb .= rt_create_breadcrumb_item( array( "title"=> $post->post_title, "rt_delimiter" => "" ) );
  114.  
  115.                     }
  116.  
  117.                     //product single posts
  118.                     elseif($post_type == 'products'){
  119.              
  120.                         //get base page
  121.                         $breadcrumb .= rt_get_base_page("products");
  122.  
  123.                         //categories of post
  124.                         $terms = wp_get_post_terms( $post->ID, "product_categories",  array( 'orderby' => 'parent', 'order' => 'DESC' ) );
  125.  
  126.                         //select only 1st one and create breadcrumb link
  127.                         if( isset( $terms[0] ) ){
  128.  
  129.                             $term_link = get_term_link( $terms[0]->slug, $terms[0]->taxonomy );
  130.                             $term_name = $terms[0]->name;
  131.  
  132.                             //check term has parents
  133.                             if( $terms[0]->parent ){
  134.                                 $breadcrumb .= rt_get_term_parents( $terms[0]->term_id, $terms[0]->taxonomy );
  135.                             }
  136.  
  137.                             //breadcrumb link of the term
  138.                             $breadcrumb .= rt_create_breadcrumb_item( array( "title"=> esc_attr( sprintf( __( "View all products in %s", "rt_theme" ), $term_name ) ), "link_name"=> esc_attr( $term_name ), "url" =>  esc_url( $term_link ) ) );
  139.  
  140.                         }
  141.                        
  142.                         //breadcrumb link of current post
  143.                         $breadcrumb .= rt_create_breadcrumb_item( array( "title"=> $post->post_title, "rt_delimiter" => "" ) );
  144.  
  145.                     }
  146.  
  147.                     //portfolio single posts
  148.                     elseif($post_type == 'portfolio'){
  149.              
  150.                         //get base page
  151.                         $breadcrumb .= rt_get_base_page("portfolio");
  152.  
  153.                         //categories of post
  154.                         $terms = wp_get_post_terms( $post->ID, "portfolio_categories",  array( 'orderby' => 'parent', 'order' => 'DESC' ) );
  155.  
  156.                         //select only 1st one and create breadcrumb link
  157.                         if( isset( $terms[0] ) ){
  158.  
  159.                             $term_link = get_term_link( $terms[0]->slug, $terms[0]->taxonomy );
  160.                             $term_name = $terms[0]->name;
  161.  
  162.                             //check term has parents
  163.                             if( $terms[0]->parent ){
  164.                                 $breadcrumb .= rt_get_term_parents( $terms[0]->term_id, $terms[0]->taxonomy );
  165.                             }
  166.  
  167.                             //breadcrumb link of the term
  168.                             $breadcrumb .= rt_create_breadcrumb_item( array( "title"=> esc_attr( sprintf( __( "View all works in %s", "rt_theme" ), $term_name ) ), "link_name"=> esc_attr( $term_name ), "url" =>  esc_url( $term_link ) ) );
  169.  
  170.                         }
  171.                        
  172.                         //breadcrumb link of current post
  173.                         $breadcrumb .= rt_create_breadcrumb_item( array( "title"=> $post->post_title, "rt_delimiter" => "" ) );
  174.  
  175.                     }      
  176.  
  177.                     //other single custom posts
  178.                     else{
  179.                        
  180.                         //breadcrumb link of current post
  181.                         $breadcrumb .= rt_create_breadcrumb_item( array( "title"=> $post->post_title, "rt_delimiter" => "" ) );
  182.  
  183.                     }  
  184.  
  185.                 }
  186.  
  187.                 //Taxonomies
  188.                 elseif ( is_tax() ){
  189.              
  190.                         //get base page for products
  191.                         if( get_query_var( 'taxonomy' ) == "product_categories" ){
  192.                             $breadcrumb .= rt_get_base_page("products");
  193.                         }
  194.  
  195.                         //get base page for portfolio
  196.                         if( get_query_var( 'taxonomy' ) == "portfolio_categories" ){
  197.                             $breadcrumb .= rt_get_base_page("portfolio");
  198.                         }          
  199.  
  200.                         $this_term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
  201.  
  202.                         $breadcrumb .= rt_get_term_parents( $this_term->term_id, $this_term->taxonomy );             
  203.                         $breadcrumb .= rt_create_breadcrumb_item( array( "title"=> esc_attr( $this_term->name ), "rt_delimiter"=>""  ) );
  204.  
  205.                 }
  206.  
  207.                 //categories
  208.                 elseif ( is_category() ){
  209.  
  210.                         //get base page
  211.                         $breadcrumb .= rt_get_base_page("blog");
  212.  
  213.                         $this_category_obj = $wp_query->get_queried_object();
  214.                         $this_category = get_category( $this_category_obj->term_id );
  215.  
  216.                         if( $this_category->parent ){
  217.                             $breadcrumb .= rt_get_category_parents( $this_category->parent );
  218.                         }
  219.  
  220.                         $breadcrumb .= rt_create_breadcrumb_item( array( "title"=> esc_attr( $this_category->name ), "rt_delimiter"=>""  ) );
  221.              
  222.  
  223.                 //404
  224.                 } elseif ( is_404() ) {
  225.  
  226.                     $breadcrumb .= rt_create_breadcrumb_item( array( "title"=> __( 'Error 404', 'rt_theme' ),  "rt_delimiter"=>""  ) );
  227.  
  228.  
  229.                 //search
  230.                 } elseif ( is_search() ) {
  231.  
  232.                     $breadcrumb .= rt_create_breadcrumb_item( array( "title"=> sprintf( __( 'Search results for: %s', 'rt_theme' ), get_search_query() ),  "rt_delimiter"=>""  ) );
  233.  
  234.                 //tag
  235.                 } elseif ( is_tag() ) {
  236.  
  237.                     $breadcrumb .= rt_create_breadcrumb_item( array( "title"=> sprintf( __( 'Posts tagged: %s', 'rt_theme' ), single_tag_title('', false) ),  "rt_delimiter"=>""  ) );
  238.  
  239.                 //author
  240.                 } elseif ( is_author() ) {
  241.  
  242.                     $breadcrumb .= rt_create_breadcrumb_item( array( "title"=> sprintf( __( 'Author: %s', 'rt_theme' ), get_the_author() ),  "rt_delimiter"=>""  ) );
  243.  
  244.                 //attachment
  245.                 } elseif ( is_attachment() ) {
  246.  
  247.  
  248.                     $this_category = get_the_category();  
  249.  
  250.                     if( $this_category ){
  251.                         $breadcrumb .= rt_get_category_parents( $this_category );
  252.                     }
  253.  
  254.                     $breadcrumb .= rt_create_breadcrumb_item( array( "title"=> $post->post_title, "rt_delimiter" => "" ) );
  255.  
  256.                 }else {
  257.  
  258.                     if( ! is_home() && ! is_front_page() ){
  259.                         $breadcrumb .= rt_create_breadcrumb_item( array( "title"=> wp_title('',false), "link_name"=> wp_title('',false), "rt_delimiter"=>""  ) );
  260.                     }
  261.                 }
  262.                
  263.  
  264.                 //close divs
  265.                 for ($i=0; $i < $rt_opened_divs ; $i++) {
  266.                     $breadcrumb .= "</div>";
  267.                 }
  268.         }
  269.  
  270.         $breadcrumb .= $wrap_after;
  271.  
  272.         $breadcrumb_menu = $breadcrumb;
  273.      
  274.         //echo breadcrumb
  275.         echo $breadcrumb_menu;
  276.     }
  277. }
  278.  
  279. // page category parents function
  280. if( ! function_exists('rt_get_term_parents') ){
  281.     function rt_get_term_parents( $term_id, $rt_taxonomy) {
  282.            
  283.             $item = "";
  284.             $the_term = get_term_by( 'id', $term_id , $rt_taxonomy );        
  285.             $get_ancestors = array_reverse( get_ancestors( $the_term->term_id, $rt_taxonomy ) );
  286.      
  287.             foreach ( $get_ancestors as $ancestor ) {
  288.                 $ancestor = get_term( $ancestor, $rt_taxonomy );
  289.                 $link = get_term_link( $ancestor->slug, $rt_taxonomy ) ;
  290.                 $name = esc_html( $ancestor->name ) ;
  291.                 $item .= rt_create_breadcrumb_item( array( "title"=> esc_attr( $name ), "link_name"=> $name, "url" =>  esc_url( $link ) ) );
  292.             }
  293.  
  294.             return $item;
  295.     }
  296. }
  297.  
  298. // page category parents function
  299. if( ! function_exists('rt_get_category_parents') ){
  300.     function rt_get_category_parents( $id, $visited = array() ) {
  301.         $chain = '';
  302.         $parent = get_term( $id, 'category' );
  303.  
  304.         if ( is_wp_error( $parent ) )
  305.             return $parent;
  306.      
  307.         $name = $parent->name;
  308.  
  309.         if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {
  310.             $visited[] = $parent->parent;
  311.             $chain .= rt_get_category_parents( $parent->parent, $visited );
  312.         }
  313.  
  314.         $chain .= rt_create_breadcrumb_item( array( "title"=> esc_attr( sprintf( __( "View all posts in %s", "rt_theme" ), $parent->name ) ), "link_name"=> esc_attr( $parent->name ), "url" =>  esc_url( get_category_link( $parent->term_id ) ) ) );
  315.  
  316.         return $chain;
  317.     }
  318. }
  319.  
  320. // page parents function
  321. if( ! function_exists('rt_create_breadcrumb_item') ){  
  322.     function rt_create_breadcrumb_item( $atts = array() ){
  323.         global $rt_delimiter, $rt_opened_divs;
  324.  
  325.         //defaults
  326.         extract(shortcode_atts(array(
  327.             "sublink" => "true",
  328.             "url"     => "",
  329.             "title"   => "",
  330.             "link_name"=> "",
  331.             "rt_delimiter" => $rt_delimiter,
  332.             "class" => ""
  333.         ), $atts));
  334.  
  335.         if ( ! isset( $rt_opened_divs ) || empty( $rt_opened_divs ) ){
  336.             $rt_opened_divs = 1;
  337.         }else{
  338.             $rt_opened_divs++;
  339.         }
  340.  
  341.         $link_name = ! empty( $link_name ) ? $link_name : $title;
  342.  
  343.         $item = "";
  344.  
  345.         $item .= ( $sublink ) ? "\n".'<div itemscope itemprop="child" itemtype="http://data-vocabulary.org/Breadcrumb">' : '<div itemscope itemtype="http://data-vocabulary.org/Breadcrumb">' ;
  346.  
  347.         $item .= ! empty( $url ) ? '<a class="'.$class.'" href="'.$url.'" itemprop="url">' : "";
  348.  
  349.         $item .= '<span itemprop="title" title="'.$title.'">' . $link_name . '</span>';
  350.  
  351.         $item .= ! empty( $url ) ? '</a>' : "";
  352.  
  353.         $item .= $rt_delimiter;
  354.  
  355.         return $item;
  356.     }
  357. }
  358.  
  359. // page parents function
  360. if( ! function_exists('rt_page_parents') ){
  361.     function rt_page_parents(){
  362.         global $post;
  363.  
  364.         $items = "";
  365.        
  366.  
  367.         //parent pages
  368.         if ( $post->post_parent ){     
  369.  
  370.             //parent page of current page
  371.             $parent_page = get_page( $post->post_parent );
  372.  
  373.             do{            
  374.                 //create breadcrum item for this page
  375.                 $items = rt_create_breadcrumb_item( array( "url"=> get_permalink( rt_wpml_page_id( $parent_page->ID ) ), "title"=> get_the_title( rt_wpml_page_id( $parent_page->ID ) ) ) ) . $items;
  376.  
  377.                 //find parent page of this page
  378.                 $parent_page = get_page( $parent_page->post_parent );
  379.  
  380.             }
  381.  
  382.             while ( empty( $parent_page->post_parent ) ) ;
  383.  
  384.         }
  385.  
  386.         //current page
  387.         $items .= rt_create_breadcrumb_item( array( "title"=> $post->post_title, "rt_delimiter" => "" ) );
  388.  
  389.         return $items;
  390.     }
  391. }
  392.  
  393. //get base page
  394. if( ! function_exists('rt_get_base_page') ){   
  395.     function rt_get_base_page( $for = "" ){
  396.  
  397.         $page = "";
  398.  
  399.         if( $for == "products" && RT_PRODUCTPAGE != "" ){
  400.  
  401.             $page = rt_create_breadcrumb_item( array( "url"=> get_permalink( rt_wpml_page_id( RT_PRODUCTPAGE ) ), "title"=> get_the_title( rt_wpml_page_id( RT_PRODUCTPAGE ) ) ) ) ;
  402.  
  403.         }elseif( $for == "portfolio" && RT_PORTFOLIOPAGE != "" ){
  404.            
  405.             $page = rt_create_breadcrumb_item( array( "url"=> get_permalink( rt_wpml_page_id( RT_PORTFOLIOPAGE ) ), "title"=> get_the_title( rt_wpml_page_id( RT_PORTFOLIOPAGE ) ) ) ) ;
  406.  
  407.         }elseif( $for == "blog" && RT_BLOGPAGE != "" ){
  408.  
  409.             $page = rt_create_breadcrumb_item( array( "url"=> get_permalink( rt_wpml_page_id( RT_BLOGPAGE ) ), "title"=> get_the_title( rt_wpml_page_id( RT_BLOGPAGE ) ) ) ) ;
  410.         }
  411.  
  412.         return $page;
  413.     }
  414. }
  415. ?>
Advertisement
Add Comment
Please, Sign In to add comment