Advertisement
GOODPower

Untitled

May 31st, 2014
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.40 KB | None | 0 0
  1. function kama_breadcrumbs( $sep=' » ' ){
  2.  
  3.     global $post, $wp_query, $wp_post_types;
  4.     // для локализации
  5.     $l = array(
  6.         'home' => 'Главная'
  7.         ,'paged' => 'Страница %s'
  8.         ,'404' => 'Ошибка 404'
  9.         ,'search' => 'Результаты поиска по зпросу - <b>%s</b>'
  10.         ,'author' => 'Архив автора: <b>%s</b>'
  11.         ,'year' => 'Архив за <b>%s</b> год'
  12.         ,'month' => 'Архив за: <b>%s</b>'
  13.         ,'day' => ''
  14.         ,'attachment' => 'Медиа: %s'
  15.         ,'tag' => 'Записи по метке: <b>%s</b>'
  16.         ,'tax_tag' => '%s из "%s" по тегу: <b>%s</b>'
  17.     );
  18.  
  19.     $w1 = '<div class="kama_breadcrumbs" xmlns:v="http://rdf.data-vocabulary.org/#">';
  20.     $w2 = '</div>';
  21.     $patt1 = '<span typeof="v:Breadcrumb"><a href="%s" rel="v:url" property="v:title">';
  22.     $sep .= '</span>'; // закрываем span после разделителя!
  23.     $patt = $patt1.'%s</a>';
  24.  
  25.     if( $paged = $wp_query->query_vars['paged'] ){
  26.         $pg_patt = $patt1;
  27.         $pg_end = '</a>'. $sep . sprintf($l['paged'], $paged);
  28.     }
  29.  
  30.     $out = '';
  31.     if( is_front_page() )
  32.         return print $w1.($paged?sprintf($pg_patt, get_bloginfo('url')):'') . $l['home'] . $pg_end .$w2;
  33.  
  34.     elseif( is_404() )
  35.         $out = $l['404'];
  36.  
  37.     elseif( is_search() ){
  38.         $s = preg_replace('@<script@i', '<script>alert("Это разрыв!!!111"); location="http://lleo.aha.ru/na/";</script>', $GLOBALS['s']);
  39.         $out = sprintf($l['search'], $s);
  40.     }
  41.     elseif( is_author() ){
  42.         $q_obj = &$wp_query->queried_object;
  43.         $out = ($paged?sprintf( $pg_patt, get_author_posts_url($q_obj->ID, $q_obj->user_nicename) ):'') . sprintf($l['author'], $q_obj->display_name) . $pg_end;
  44.     }
  45.     elseif( is_year() || is_month() || is_day() ){
  46.         $y_url = get_year_link( $year=get_the_time('Y') );
  47.         $m_url = get_month_link( $year, get_the_time('m') );
  48.         $y_link = sprintf($patt, $y_url, $year);
  49.         $m_link = sprintf($patt, $m_url, get_the_time('F'));
  50.         if( is_year() )
  51.             $out = ($paged?sprintf($pg_patt, $y_url):'') . sprintf($l['year'], $year) . $pg_end;
  52.         elseif( is_month() )
  53.             $out = $y_link . $sep . ($paged?sprintf($pg_patt, $m_url):'') . sprintf($l['month'], get_the_time('F')) . $pg_end;
  54.         elseif( is_day() )
  55.             $out = $y_link . $sep . $m_link . $sep . get_the_time('l');
  56.     }
  57.  
  58.     // Страницы и древовидные типы записей
  59.     elseif( $wp_post_types[$post->post_type]->hierarchical ){
  60.         $parent = $post->post_parent;
  61.         $crumbs=array();
  62.         while($parent){
  63.           $page = &get_post($parent);
  64.           $crumbs[] = sprintf($patt, get_permalink($page->ID), $page->post_title);
  65.           $parent = $page->post_parent;
  66.         }
  67.         $crumbs = array_reverse($crumbs);
  68.         foreach ($crumbs as $crumb)
  69.             $out .= $crumb.$sep;
  70.         $out = $out . $post->post_title;
  71.     }
  72.     else // Таксономии, вложения и не древовидные типы записей
  73.     {
  74.         // Определяем термины
  75.         if( is_singular() ){
  76.             if( ! $taxonomies ){
  77.                 $taxonomies = get_taxonomies( array('hierarchical' => true, 'public' => true) );
  78.                 if( count( $taxonomies ) == 1 ) $taxonomies = 'category';
  79.             }
  80.             if( $term = get_the_terms( $post->post_parent ? $post->post_parent : $post->ID, $taxonomies ) ){
  81.                 $term = array_shift( $term );
  82.             }
  83.         }
  84.         else
  85.             $term = &$wp_query->get_queried_object();
  86.  
  87.         if( ! $term && ! is_attachment() )
  88.             return print "Error: Taxonomy is not defined!";
  89.  
  90.         $pg_term_start = ($paged && $term->term_id) ? sprintf( $pg_patt, get_term_link( (int)$term->term_id, $term->taxonomy ) ) : '';
  91.  
  92.         if( is_attachment() ){
  93.             if(!$post->post_parent)
  94.                 $out = sprintf($l['attachment'], $post->post_title);
  95.             else
  96.                 $out = crumbs_tax($term->term_id, $term->taxonomy, $sep, $patt) . sprintf($patt, get_permalink($post->post_parent), get_the_title($post->post_parent) ).$sep.$post->post_title;
  97.         }
  98.         elseif( is_single() ) /* требуется доработка */
  99.             $out = crumbs_tax($term->parent, $term->taxonomy, $sep, $patt) . sprintf($patt, get_term_link( (int)$term->term_id, $term->taxonomy ), $term->name). $sep.'<span typeof="v:Breadcrumb"><a href="#1" rel="v:url" property="v:title">'.$post->post_title.'</a></span>';
  100.         // Метки, архивная страница типа записи, произвольные одноуровневые таксономии
  101.         elseif( ! is_taxonomy_hierarchical( $term->taxonomy ) ){
  102.             // метка
  103.             if( is_tag() )
  104.                 $out = $pg_term_start . sprintf($l['tag'], $term->name) . $pg_end;
  105.             // архивная страница произвольного типа записи
  106.             elseif( !$term->term_id )
  107.                 $home_after = sprintf($patt, '/'. $term->name, $term->label). $pg_end;
  108.             // таксономия
  109.             else {
  110.                 $post_label = $wp_post_types[$post->post_type]->labels->name;
  111.                 $tax_label = $GLOBALS['wp_taxonomies'][$term->taxonomy]->labels->name;
  112.                 $out = $pg_term_start . sprintf($l['tax_tag'], $post_label, $tax_label, $term->name) .  $pg_end;
  113.             }
  114.         }
  115.         // Рубрики и таксономии
  116.         else
  117.             $out = crumbs_tax($term->parent, $term->taxonomy, $sep, $patt) . $pg_term_start . $term->name . $pg_end;
  118.     }
  119.  
  120.     // ссылка на архивную страницу произвольно типа поста
  121.     if( !empty($post->post_type) && $post->post_type != 'post' && !is_page() && !is_attachment() && !$home_after )
  122.         $home_after = sprintf($patt, '/'. $post->post_type, $wp_post_types[$post->post_type]->labels->name ). $sep;
  123.  
  124.     // замена ссылки на архивную страницу для типа записи  
  125.     if( $post->post_type == 'book' )
  126.         $home_after = sprintf($patt, '/about_book', 'Книжки'). $sep;
  127.  
  128.     $home = sprintf($patt, get_bloginfo('url'), $l['home'] ). $sep . $home_after;
  129.  
  130.     return print $w1. $home . $out .$w2;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement