Advertisement
alexgieg

WPSEO Breadcrumbs

Jun 18th, 2012
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.75 KB | None | 0 0
  1.     function breadcrumb($prefix = '', $suffix = '', $display = true) {
  2.         $options = get_wpseo_options();
  3.  
  4.         global $wp_query, $post, $paged;
  5.  
  6.         $opt        = get_option("wpseo_internallinks");
  7.         $on_front   = get_option('show_on_front');
  8.         $blog_page  = get_option('page_for_posts');
  9.         $sep        = ( isset($opt['breadcrumbs-sep']) && $opt['breadcrumbs-sep'] != '' ) ? $opt['breadcrumbs-sep'] : '»';
  10.         $hometitle  = ( isset($opt['breadcrumbs-home']) && $opt['breadcrumbs-home'] != '' ) ? $opt['breadcrumbs-home'] : __('Home','wordpress-seo');
  11.         // This doesn't seem to be used:
  12.         $selmenu    = ( isset($opt['breadcrumbs-selectedmenu']) && $opt['breadcrumbs-selectedmenu'] != '' ) ? $opt['breadcrumbs-selectedmenu'] : 0;
  13.         $breadcrumb_array   = array();
  14.        
  15.         if ( "page" == $on_front && 'post' == get_post_type() ) {
  16.             $homeurl = get_permalink(get_option('page_on_front'));
  17.         } else {
  18.             $homeurl = get_bloginfo('url');
  19.         }
  20.  
  21.         if ( $on_front == "page" && is_front_page() ) {
  22.             $breadcrumb_array[] = array( 'title' => $hometitle, 'url' => '', 'class' => '', 'type' => 'home', 'ID' => null );
  23.         } else if ( $on_front == "posts" && is_home() ) {
  24.             $breadcrumb_array[] = array( 'title' => $hometitle, 'url' => '', 'class' => '', 'type' => 'home', 'ID' => null );
  25.         } else if ( $on_front == "page" && is_home() ) {
  26.             $breadcrumb_array[] = array( 'title' => $hometitle, 'url' => $homeurl, 'class' => '', 'type' => 'home', 'ID' => null );
  27.             $breadcrumb_array[] = array( 'title' => $this->get_bc_title($blog_page), 'url' => '', 'class' => '', 'type' => get_post_type($blog_page), 'ID' => (int) $blog_page );
  28.         } else if ( is_singular() ) {
  29.             $breadcrumb_array[] = array( 'title' => $hometitle, 'url' => $homeurl, 'class' => '', 'type' => 'home', 'ID' => null );
  30.            
  31.             if ( $blog_page && 'page' == $on_front && 'post' == get_post_type() && ( !isset($opt['breadcrumbs-blog-remove']) || !$opt['breadcrumbs-blog-remove'] ) ) {
  32.                 $breadcrumb_array[] = array( 'title' => $this->get_bc_title($blog_page), 'url' => get_permalink($blog_page), 'class' => '', 'type' => get_post_type($blog_page), 'ID' => (int) $blog_page );
  33.             }
  34.  
  35.             // This doesn't seem to be used:
  36.             if( isset($opt['breadcrumbs-menus']) && $opt['breadcrumbs-menus'] = 'on'){
  37.                 $use_menu = $this->in_menu( $selmenu );
  38.             }
  39.             if ( function_exists('bbp_body_class') && count( bbp_body_class( array() ) ) > 1 ) {
  40.                 remove_filter('bbp_get_breadcrumb','__return_false');
  41.                 $bbp_bc_array = explode( ';;;;;', bbp_get_breadcrumb( ';;;;;' ) );
  42.                 if ( !empty($bbp_bc_array[0]) ) {
  43.                     foreach ( $bbp_bc_array as $bbp_bc ) {
  44.                         if ( preg_match( '/(<(a .*href="(?P<url>.*?)"( .*class="(?P<class>.*?)")?.*?)>)?(?P<title>.*)/', trim($bbp_bc), $bbp_pc_results ) ) {
  45.                             $breadcrumb_array[] = array ( 'title' => trim(strip_tags($bbp_pc_results['title'])), 'url' => $bbp_pc_results['url'], 'class' => $bbp_pc_results['class'], 'type' => 'bbpress', 'ID' => null );
  46.                         }
  47.                     }
  48.                 }
  49.                 add_filter('bbp_get_breadcrumb','__return_false');
  50.             } else if( isset( $use_menu ) && $use_menu ){
  51.                 // This whole block doesn't seem to be used:
  52.                 $trail = $this->get_menu_trail();
  53.                 $trail = array_reverse ( $trail );
  54.                 $trailposts = array();
  55.                 for($t = 0; $t < count($trail); $t++){
  56.                     $trailposts[] = $this->get_post_for_menunode($trail[$t]);
  57.                 }
  58.                 for($t = 0; $t < count($trail); $t++){
  59.                     $bctitle = ( get_the_title( $trail[$t] ) == '' ) ? get_the_title( $trailposts[$t] ) : get_the_title( $trail[$t] );
  60.                     $breadcrumb_array[] = array( 'title' => $bctitle, 'url' => get_permalink( $trailposts[$t] ), 'class' => '', 'type' => get_post_type( $trailposts[$t] ), 'ID' => (int) $trailposts[$t] );
  61.                 }
  62.                 $breadcrumb_array[] = array( 'title' => $this->get_bc_title( $post->ID ), 'url' => '', 'class' => '', 'type' => get_post_type( $post->ID ), 'ID' => (int) $post->ID );
  63.             } else {
  64.                 $post_type = get_post_type();
  65.                 if ( function_exists('get_post_type_archive_link') && get_post_type_archive_link( $post_type ) ) {
  66.                     if ( isset($options['bctitle-ptarchive-'.$post_type]) && '' != $options['bctitle-ptarchive-'.$post_type] ) {
  67.                         $archive_title = $options['bctitle-ptarchive-'.$post_type];
  68.                     } else {
  69.                         $post_type_obj = get_post_type_object( $post_type );
  70.                         //$archive_title = $post_type_obj->labels->menu_name;
  71.                         $archive_title = $post_type_obj->labels->name;
  72.                     }
  73.                     $breadcrumb_array[] = array( 'title' => $archive_title, 'url' => get_post_type_archive_link( $post_type ), 'class' => '', 'type' => $post_type, 'ID' => null );
  74.                 }
  75.                
  76.                 if ( 0 == $post->post_parent ) {
  77.                     if ( isset( $opt['post_types-'.$post->post_type.'-maintax'] ) && $opt['post_types-'.$post->post_type.'-maintax'] != '0' ) {
  78.                         $main_tax = $opt['post_types-'.$post->post_type.'-maintax'];
  79.                         $terms = wp_get_object_terms( $post->ID, $main_tax, apply_filters( 'wp_seo_get_bc_taxonomy_args', array( 'orderby' => 'term_order' ) ) );
  80.                         if ( count($terms) > 0 ) {
  81.                             if ( is_taxonomy_hierarchical($main_tax) && $terms[0]->parent != 0 ) {
  82.                                 $parents = $this->get_term_parents($terms[0], $main_tax);
  83.                                 $parents = array_reverse($parents);
  84.                                 foreach($parents as $parent) {
  85.                                     $bctitle = wpseo_get_term_meta( $parent, $main_tax, 'bctitle' );
  86.                                     if (!$bctitle) {
  87.                                         $bctitle = $parent->name;
  88.                                     }
  89.                                     $breadcrumb_array[] = array( 'title' => $bctitle, 'url' => get_term_link( $parent, $main_tax ), 'class' => '', 'type' => $main_tax, 'ID' => (int) $parent->term_id );
  90.                                 }
  91.                             }
  92.                             $bctitle = wpseo_get_term_meta( $terms[0], $main_tax, 'bctitle' );
  93.                             if (!$bctitle) {
  94.                                 $bctitle = $terms[0]->name;
  95.                             }
  96.                             $breadcrumb_array[] = array( 'title' => $bctitle, 'url' => get_term_link($terms[0], $main_tax), 'class' => '', 'type' => $main_tax, 'ID' => (int) $terms[0]->term_id );
  97.                         }
  98.                     }
  99.                     $breadcrumb_array[] = array( 'title' => $this->get_bc_title( $post->ID ), 'url' => '', 'class' => '', 'type' => $post_type, 'ID' => (int) $post->ID );
  100.                 } else {
  101.                     if (isset($post->ancestors)) {
  102.                         if (is_array($post->ancestors)) {
  103.                             $ancestors = array_values($post->ancestors);
  104.                         } else {
  105.                             $ancestors = array($post->ancestors);
  106.                         }
  107.                     } else {
  108.                         $ancestors = array($post->post_parent);
  109.                     }
  110.  
  111.                     $ancestors = array_reverse( $ancestors ); // Reverse the order so it's oldest to newest
  112.                     foreach ( $ancestors as $ancestor ) {
  113.                         $breadcrumb_array[] = array( 'title' => $this->get_bc_title( $ancestor ), 'url' => get_permalink($ancestor), 'class' => '', 'type' => get_post_type($ancestor), 'ID' => (int) $ancestor );
  114.                     }
  115.  
  116.                     $breadcrumb_array[] = array( 'title' => $this->get_bc_title( $post->ID ), 'url' => '', 'class' => '', 'type' => $post_type, 'ID' => (int) $post->ID );
  117.                 }
  118.             }
  119.         } else {
  120.             $breadcrumb_array[] = array( 'title' => $hometitle, 'url' => $homeurl, 'class' => '', 'type' => 'home', 'ID' => null );
  121.             if ( !is_404() && $blog_page && 'page' == $on_front && 'post' == get_post_type() && ( !isset($opt['breadcrumbs-blog-remove']) || !$opt['breadcrumbs-blog-remove'] ) ) {
  122.                 $breadcrumb_array[] = array( 'title' => $this->get_bc_title($blog_page), 'url' => get_permalink($blog_page), 'class' => '', 'type' => get_post_type($blog_page), 'ID' => (int) $blog_page );
  123.             }
  124.             if ( function_exists('is_post_type_archive') && is_post_type_archive() && get_post_type() ) {
  125.                 $post_type = get_post_type();
  126.                 if ( isset($options['bctitle-ptarchive-'.$post_type]) && '' != $options['bctitle-ptarchive-'.$post_type] ) {
  127.                     $archive_title = $options['bctitle-ptarchive-'.$post_type];
  128.                 } else {
  129.                     $post_type_obj = get_post_type_object( $post_type );
  130.                     //$archive_title = $post_type_obj->labels->menu_name;
  131.                     $archive_title = $post_type_obj->labels->name;
  132.                 }
  133.                 $breadcrumb_array[] = array( 'title' => $archive_title, 'url' => '', 'class' => '', 'type' => $post_type, 'ID' => null );
  134.             } else if ( is_tax() || is_tag() || is_category() ) {
  135.                 $term = $wp_query->get_queried_object();
  136.  
  137.                 if ( isset($options['taxonomy-'.$term->taxonomy.'-ptparent']) && $options['taxonomy-'.$term->taxonomy.'-ptparent'] != '' ) {
  138.                     $post_type = $options['taxonomy-'.$term->taxonomy.'-ptparent'];
  139.                     if ( $blog_page && 'post' == $post_type && 'page' == $on_front  ) {
  140.                         $breadcrumb_array[] = array( 'title' => $this->get_bc_title($blog_page), 'url' => get_permalink($blog_page), 'class' => '', 'type' => $post_type, 'ID' => $blog_page );
  141.                     } else {
  142.                         if ( isset($options['bctitle-ptarchive-'.$post_type]) && '' != $options['bctitle-ptarchive-'.$post_type] ) {
  143.                             $archive_title = $options['bctitle-ptarchive-'.$post_type];
  144.                         } else {
  145.                             $post_type_obj = get_post_type_object( $post_type );
  146.                             //$archive_title = $post_type_obj->labels->menu_name;
  147.                             $archive_title = $post_type_obj->labels->name;
  148.                         }
  149.                         $breadcrumb_array[] = array( 'title' => $archive_title, 'url' => get_post_type_archive_link( $post_type ), 'class' => '', 'type' => $post_type, 'ID' => null );
  150.                     }
  151.                 }
  152.                
  153.                 if ( is_taxonomy_hierarchical($term->taxonomy) && $term->parent != 0 ) {
  154.                     $parents = $this->get_term_parents($term, $term->taxonomy);
  155.                     $parents = array_reverse( $parents );
  156.                    
  157.                     foreach($parents as $parent) {
  158.                         $bctitle = wpseo_get_term_meta( $parent, $term->taxonomy, 'bctitle' );
  159.                         if (!$bctitle) {
  160.                             $bctitle = $parent->name;
  161.                         }
  162.                         $breadcrumb_array[] = array( 'title' => $bctitle, 'url' => get_term_link( $parent, $term->taxonomy ), 'class' => '', 'type' => $parent->taxonomy, 'ID' => (int) $parent->term_id );
  163.                     }
  164.                 }
  165.  
  166.                 $bctitle = wpseo_get_term_meta( $term, $term->taxonomy, 'bctitle' );
  167.                 if (!$bctitle) {
  168.                     $bctitle = $term->name;
  169.                 }
  170.                
  171.                 if ($paged) {
  172.                     $breadcrumb_array[] = array( 'title' => $bctitle, 'url' => get_term_link( $term, $term->taxonomy ), 'class' => '', 'type' => $term->taxonomy, 'ID' => (int) $term->term_id );
  173.                 } else {
  174.                     $breadcrumb_array[] = array( 'title' => $bctitle, 'url' => '', 'class' => '', 'type' => $term->taxonomy, 'ID' => (int) $term->term_id );
  175.                 }
  176.             } else if ( is_date() ) {
  177.                 if ( isset($opt['breadcrumbs-archiveprefix']) ) {
  178.                     $bc = $opt['breadcrumbs-archiveprefix'];
  179.                 } else {
  180.                     $bc = __('Archives for','wordpress-seo');
  181.                 }
  182.                 if ( is_day() ) {
  183.                     global $wp_locale;
  184.                     $breadcrumb_array[] = array( 'title' => $wp_locale->get_month( get_query_var('monthnum') ).' '.get_query_var('year'), 'url' => get_month_link( get_query_var('year'), get_query_var('monthnum') ), 'class' => '', 'type' => 'date', 'ID' => null );
  185.                     $breadcrumb_array[] = array( 'title' => $bc." ".get_the_date(), 'url' => '', 'class' => '', 'type' => 'date', 'ID' => null );
  186.                 } else if ( is_month() ) {
  187.                     $breadcrumb_array[] = array( 'title' => $bc." ".single_month_title(' ',false), 'url' => '', 'class' => '', 'type' => 'date', 'ID' => null );
  188.                 } else if ( is_year() ) {
  189.                     $breadcrumb_array[] = array( 'title' => $bc." ".get_query_var('year'), 'url' => '', 'class' => '', 'type' => 'date', 'ID' => null );
  190.                 }
  191.             } elseif ( is_author() ) {
  192.                 if ( isset($opt['breadcrumbs-archiveprefix']) ) {
  193.                     $bc = $opt['breadcrumbs-archiveprefix'];
  194.                 } else {
  195.                     $bc = __('Archives for','wordpress-seo');
  196.                 }
  197.                 $user = $wp_query->get_queried_object();
  198.                 $breadcrumb_array[] = array( 'title' => $bc." ".$user->display_name, 'url' => '', 'class' => '', 'type' => 'author', 'ID' => $user->ID );
  199.             } elseif ( is_search() ) {
  200.                 if ( isset($opt['breadcrumbs-searchprefix']) && $opt['breadcrumbs-searchprefix'] != '' ) {
  201.                     $bc = $opt['breadcrumbs-searchprefix'];
  202.                 } else {
  203.                     $bc = __('You searched for','wordpress-seo');
  204.                 }
  205.                 $breadcrumb_array[] = array( 'title' => $bc.' "'.stripslashes(strip_tags(get_search_query())).'"', 'url' => '', 'class' => '', 'type' => 'search', 'ID' => null );
  206.             } elseif ( isset( $wp_query->query_vars['bbp_topic_tag'] ) ) {
  207.                 remove_filter('bbp_get_breadcrumb','__return_false');
  208.                 $bbp_bc_array = explode( ';;;;;', bbp_get_breadcrumb( ';;;;;' ) );
  209.                 if ( !empty($bbp_bc_array[0]) ) {
  210.                     foreach ( $bbp_bc_array as $bbp_bc ) {
  211.                         if ( preg_match( '/(<(a .*href="(?P<url>.*?)"( .*class="(?P<class>.*?)")?.*?)>)?(?P<title>.*)/', trim($bbp_bc), $bbp_pc_results ) ) {
  212.                             $breadcrumb_array[] = array ( 'title' => trim(strip_tags($bbp_pc_results['title'])), 'url' => $bbp_pc_results['url'], 'class' => $bbp_pc_results['class'], 'type' => 'bbpress', 'ID' => null );
  213.                         }
  214.                     }
  215.                 }
  216.                 add_filter('bbp_get_breadcrumb','__return_false');
  217.             } elseif ( is_404() ) {
  218.                 if ( isset($opt['breadcrumbs-404crumb']) && $opt['breadcrumbs-404crumb'] != '' ) {
  219.                     $crumb404 = $opt['breadcrumbs-404crumb'];
  220.                 } else {
  221.                     $crumb404 = __('Error 404: Page not found','wordpress-seo');
  222.                 }
  223.                 $breadcrumb_array[] = array ( 'title' => $crumb404, 'url' => '', 'class' => '', 'type' => '404-not-found', 'ID' => null );
  224.             }
  225.         }
  226.        
  227.         $breadcrumb_array = apply_filters( 'wp_seo_get_bc_ancestors', $breadcrumb_array );
  228.  
  229.         $output = '';
  230.         if ( !empty($breadcrumb_array) ) {
  231.             $output .= $prefix;
  232.             if ( isset($opt['breadcrumbs-prefix']) && $opt['breadcrumbs-prefix'] != "" ) {
  233.                 $output .= $opt['breadcrumbs-prefix'].' ';
  234.             }
  235.  
  236.             $breadcrumb_total = count( $breadcrumb_array ) - 1; // Since arrays start at 0.
  237.             for ( $i = 0 ; $i <= $breadcrumb_total ; $i++ ) {
  238.                 $breadcrumb = $breadcrumb_array[$i];
  239.                
  240.                 $pre_out = $post_out = '';
  241.                 $mid_out = $breadcrumb['title'];
  242.                
  243.                 // We don't want search engines displaying the home link in the snippet breadcrumb. Also,
  244.                 // a breadcrumb DIV for the very last item (the current page) is ignored. So we deal with
  245.                 // the special cases:
  246.                 if ( ( $i == 0 && $breadcrumb['type'] != 'home' ) || ( $i > 0 && $i < $breadcrumb_total ) ) {
  247.                     $pre_out .= '<div';
  248.                     if ( ( $i == 1 && $breadcrumb_array[0]['type'] != 'home' ) || $i > 1 ) {
  249.                         $pre_out .= ' itemprop="child"';
  250.                     }
  251.                     $pre_out .= ' itemscope itemtype="http://data-vocabulary.org/Breadcrumb" style="display: inline;">';
  252.                     $mid_out = '<span itemprop="title">'.$mid_out.'</span>';
  253.                     $suffix = '</div>'.$suffix;
  254.                 }
  255.                
  256.                 if ( $breadcrumb['url'] ) {
  257.                     $pre_out .= '<a href="'.esc_url($breadcrumb['url']).'"';
  258.                     if ( $breadcrumb['class'] ) {
  259.                         $pre_out .= ' class="'.esc_attr($breadcrumb['class']).'"';
  260.                     }
  261.                     if ( $i == 0 && $breadcrumb['type'] != 'home' ) {
  262.                         $pre_out .= ' itemprop="url"';
  263.                     }
  264.                     $pre_out .= '>';
  265.                     $post_out .= '</a>';
  266.                 }
  267.                
  268.                 if ( $i < $breadcrumb_total ) {
  269.                     $post_out .= ' '.$sep.' ';
  270.                 } else {
  271.                     $mid_out = $this->bold_or_not( $mid_out );
  272.                 }
  273.                
  274.                 $output .= $pre_out.$mid_out.$post_out;
  275.             }
  276.             $output .= $suffix;
  277.         }
  278.        
  279.         if ($display) {
  280.             echo $output;
  281.             return true;
  282.         } else {
  283.             return $output;
  284.         }
  285.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement