Wordpress bredcrumbs function

<?php /** * Wordpress bredcrumbs (breadcrumbs) * * $sep - separator. Default one ' » ' * $l10n - array for localization. You can look at variable $default_l10n. * $args - array. additional arguments. * version 1 */ function kama_breadcrumbs( $sep = '', $l10n = array(), $args = array() ){ global $post, $wp_query, $wp_post_types; // Локализация $default_l10n = array( 'home' => 'Главная', 'paged' => 'Страница %d', '_404' => 'Ошибка 404', 'search' => 'Результаты поиска по запросу - <b>%s</b>', 'author' => 'Архив автора: <b>%s</b>', 'year' => 'Архив за <b>%d</b> год', 'month' => 'Архив за: <b>%d</b>', 'day' => '', 'attachment' => 'Медиа: %s', 'tag' => 'Записи по метке: <b>%s</b>', 'tax_tag' => '%1$s из "%2$s" по тегу: <b>%3$s</b>', // tax_tag выведет: 'тип_записи из "название_таксы" по тегу: имя_термина'. // Если нужны отдельные холдеры, например только имя термина, пишем так: 'записи по тегу: %3$s' ); // Параметры по умолчанию $default_args = array( 'on_front_page' => true, // выводить крошки на главной странице 'show_post_title' => true, // показывать ли название записи в конце (последний элемент). Для записей, страниц, вложений // можно указать строку вида <span>%s</span>, когда нужно обернуть заголовок в html 'sep' => ' » ', // разделитель 'markup' => 'schema.org', // микроразметка. Может быть: rdf.data-vocabulary.org или schema.org 'priority_tax' => array('category'), // приоритетные таксономии, нужно когда запись в нескольких таксах 'priority_terms' => array(), // 'priority_terms' - приоритетные элементы таксономий, когда запись находится в нескольких элементах одной таксы одновременно. // Например: array( 'category'=>array(45,'term_name'), 'tax_name'=>array(1,2,'name') ) // 'category' - такса для которой указываются приор. элементы: 45 - ID термина и 'term_name' - ярлык. // порядок 45 и 'term_name' имеет значение: чем раньше тем важнее. Все указанные термины важнее неуказанных... ); // Фильтрует аргументы по умолчанию. $default_args = apply_filters('kama_breadcrumbs_default_args', $default_args ); $loc = (object) array_merge( $default_l10n, $l10n ); $args = (object) array_merge( $default_args, $args ); if( ! $sep ) $sep = $args->sep; // микроразметка --- // rdf.data-vocabulary.org if( $args->markup == 'rdf.data-vocabulary.org' ){ $w1 = '<div class="kama_breadcrumbs" prefix="v: http://rdf.data-vocabulary.org/#">'; $w2 = '</div>'; $patt1 = '<span typeof="v:Breadcrumb"><a href="%s" rel="v:url" property="v:title">'; $sep .= '</span>'; // закрываем span после разделителя! $linkpatt = $patt1.'%s</a>'; } // schema.org elseif( $args->markup == 'schema.org' ){ $w1 = '<div class="kama_breadcrumbs" vocab="http://schema.org/" typeof="BreadcrumbList">'; $w2 = '</div>'; $patt1 = '<span property="itemListElement" typeof="ListItem"><a href="%s" property="item" typeof="WebPage"><span property="name">'; $sep .= '</span>'; // закрываем span после разделителя! $linkpatt = $patt1.'%s</span></a>'; } $ptype = & $wp_post_types[ $post->post_type ]; // OUTPUT $pg_end = ''; if( $paged_num = $wp_query->query_vars['paged'] ){ $pg_patt = $patt1; $pg_end = '</a>'. $sep . sprintf( $loc->paged, (int) $paged_num ); } $out = ''; if( is_front_page() ){ return $args->on_front_page ? ( print $w1 .( $paged_num ? sprintf($pg_patt, get_home_url()) : '' ) . $loc->home . $pg_end . $w2 ) : ''; } elseif( is_404() ){ $out = $loc->_404; } elseif( is_search() ){ $out = sprintf( $loc->search, esc_html( $GLOBALS['s'] ) ); } elseif( is_author() ){ $q_obj = &$wp_query->queried_object; $out = ( $paged_num ? sprintf( $pg_patt, get_author_posts_url( $q_obj->ID, $q_obj->user_nicename ) ):'') . sprintf( $loc->author, esc_html($q_obj->display_name) ) . $pg_end; } elseif( is_year() || is_month() || is_day() ){ $y_url = get_year_link( $year = get_the_time('Y') ); $m_url = get_month_link( $year, get_the_time('m') ); $y_link = sprintf( $linkpatt, $y_url, $year); $m_link = sprintf( $linkpatt, $m_url, get_the_time('F')); if( is_year() ) $out = ( $paged_num ? sprintf($pg_patt, $y_url) : '' ) . sprintf( $loc->year, $year ) . $pg_end; elseif( is_month() ) $out = $y_link . $sep . ( $paged_num ? sprintf( $pg_patt, $m_url ) : '') . sprintf( $loc->month, get_the_time('F') ) . $pg_end; elseif( is_day() ) $out = $y_link . $sep . $m_link . $sep . get_the_time('l'); } // Древовидные записи elseif( is_singular() && $ptype->hierarchical ){ $parent = $post->post_parent; $crumbs = array(); while( $parent ){ $page = get_post( $parent ); $crumbs[] = sprintf( $linkpatt, get_permalink( $page->ID ), $page->post_title ); $parent = $page->post_parent; } $crumbs = array_reverse( $crumbs ); foreach( $crumbs as $crumb ) $out .= $crumb . $sep; $out = $out . __show_post_title( $args->show_post_title, $post->post_title ); } // Таксы, вложения и недревовидные записи else { $term = false; // set term (attachments too) if( is_singular() ){ // Чтобы определить термин для вложения if( is_attachment() ){ if( $post->post_parent ){ $save_post = $post; $post = get_post( $post->post_parent ); } } // учитывает есил вложения прикрепляются к таксам древовидным - все бывает :) $taxonomies = get_object_taxonomies( $post->post_type ); // оставим только древовидные и публичные, мало ли... $taxonomies = array_intersect( $taxonomies, get_taxonomies( array('hierarchical' => true, 'public' => true) ) ); // не делаем лишнего... if( $taxonomies ){ // пробуем найти приоритетные $priority_tax = array_intersect( $taxonomies, $args->priority_tax ); // получаем название таксы $taxonomy = $priority_tax ? array_shift( $priority_tax ) : array_shift( $taxonomies ); if( $terms = get_the_terms( $post->ID, $taxonomy ) ){ $term = array_shift( $terms ); // проверим приоритетные термины для таксы $prior_terms = & $args->priority_terms[ $taxonomy ]; if( $prior_terms && count($terms) > 1 ){ foreach( (array) $prior_terms as $term_id ){ $filter_field = is_numeric($term_id) ? 'term_id' : 'slug'; $_terms = wp_list_filter( $terms, array($filter_field=>$term_id) ); if( $_terms ){ $term = array_shift( $_terms ); break; } } } } } if( isset($save_post) ) $post = $save_post; // вернем обратно (для вложений) } // term for tax page else $term = get_queried_object(); //if( ! $term && ! is_attachment() ) return print "Error: Taxonomy is not defined!"; //var_dump($term); if( $term ){ $term = apply_filters('kama_breadcrumbs_term', $term ); $pg_term_start = ( $paged_num && $term->term_id ) ? sprintf( $pg_patt, get_term_link( (int) $term->term_id, $term->taxonomy ) ) : ''; // attachment if( is_attachment() ){ if( ! $post->post_parent ) $out = sprintf( $loc->attachment, esc_html($post->post_title) ); else $out = __crumbs_tax( $term->term_id, $term->taxonomy, $sep, $linkpatt ) . sprintf( $linkpatt, get_permalink( $post->post_parent ), get_the_title( $post->post_parent ) ) . $sep . __show_post_title( $args->show_post_title, $post->post_title ); } // single elseif( is_single() ){ $out = __crumbs_tax( $term->parent, $term->taxonomy, $sep, $linkpatt ) . sprintf( $linkpatt, get_term_link( (int) $term->term_id, $term->taxonomy ), $term->name ). $sep . __show_post_title( $args->show_post_title, $post->post_title ); // Метки, архивная страница типа записи, произвольные одноуровневые таксономии } // taxonomy не древовидная elseif( ! is_taxonomy_hierarchical( $term->taxonomy ) ){ // метка if( is_tag() ) $out = $pg_term_start . sprintf( $loc->tag, $term->name ) . $pg_end; // таксономия elseif( is_tax() ){ $post_label = $ptype->labels->name; $tax_label = $GLOBALS['wp_taxonomies'][ $term->taxonomy ]->labels->name; $out = $pg_term_start . sprintf( $loc->tax_tag, $post_label, $tax_label, $term->name ) . $pg_end; } } // Рубрики и таксономии else{ //die( $term->taxonomy ); $out = __crumbs_tax( $term->parent, $term->taxonomy, $sep, $linkpatt ) . $pg_term_start . $term->name . $pg_end; } } } $home_after = ''; // замена ссылки на архивную страницу для типа записи $home_after = apply_filters('kama_breadcrumbs_home_after', false, $linkpatt, $sep ); // Cсылка на архивную страницу произвольно типа поста. Ссылку можно заменить с помощью хука 'kama_breadcrumbs_home_after' if( ! $home_after && $ptype->has_archive && (is_post_type_archive() || is_singular()) && ! in_array( $post->post_type, array('post','page','attachment') ) ){ $pt_name = $ptype->labels->name; if( is_post_type_archive() && ! $paged_num ) $home_after = $pt_name; else $home_after = sprintf( $linkpatt, get_post_type_archive_link( $post->post_type ), $pt_name ) . ($pg_end ? $pg_end : $sep); } $home = sprintf( $linkpatt, home_url(), $loc->home ). $sep . $home_after; $out = apply_filters('kama_breadcrumbs_pre_out', $out ); $out = $w1. $home . $out .$w2; return print apply_filters('kama_breadcrumbs', $out, $sep ); } function __show_post_title( $is_show, $title ){ return $is_show ? ( is_string($is_show) ? sprintf( $is_show, esc_html($title) ) : esc_html($title) ) : ''; } function __crumbs_tax( $term_id, $tax, $sep, $linkpatt ){ $termlink = array(); while( (int) $term_id ){ $term2 = get_term( $term_id, $tax ); $termlink[] = sprintf( $linkpatt, get_term_link( (int) $term2->term_id, $term2->taxonomy ), esc_html($term2->name) ). $sep; $term_id = (int) $term2->parent; } $termlinks = array_reverse( $termlink ); return implode('', $termlinks ); } ?>
<?php if( function_exists('kama_breadcrumbs') ) kama_breadcrumbs(); ?>

If you need to change separator:

<?php if( function_exists('kama_breadcrumbs') ) kama_breadcrumbs(' » '); ?>

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.