Skip to content

Instantly share code, notes, and snippets.

@zaurmag
Last active July 5, 2017 10:36
Show Gist options
  • Save zaurmag/afa7e116a39b58399fdc70adf036814b to your computer and use it in GitHub Desktop.
Save zaurmag/afa7e116a39b58399fdc70adf036814b to your computer and use it in GitHub Desktop.
Хлебные крошки Wordpress
<?php
/*** Функция хлебных крошек ***/
function breadcrumbs_new() {
$mayak_home = 'Главная'; // текст ссылки "Главная"
$mayak_last_crumb = 1; // 1 - показывать название текущей статьи/страницы/рубрики, 0 - не показывать
$mayak_between = ' <span class="crumb_separator">&raquo;</span> ';
$mayak_from = '<span class="current-crumbs">';
$mayak_to = '</span>';
global $post;
$mayak_link_to_home = home_url('/');
$mayak_from_court = '<span typeof="v:Breadcrumb">';
$mayak_to_court = '</span>';
$mayak_link_rdf = ' rel="v:url" property="v:title"';
$mayak_path = $mayak_from_court . '<a' . $mayak_link_rdf . ' href="%1$s">%2$s</a>' . $mayak_to_court;
$mayak_superior = $descendant = $post->post_parent;
$mayak_initial_page = get_option('page_on_front');
if (is_home() || is_front_page()) {
echo '<nav class="block-crumbs hidden-md-down"><a href="'. $mayak_link_to_home.'">' . $mayak_home . '</a></nav>';
} else {
echo '<nav class="block-crumbs hidden-md-down" xmlns:v="http://rdf.data-vocabulary.org/#">';
{
echo '<a href="'. $mayak_link_to_home.'" rel="v:url" property="v:title">' . $mayak_home . '</a>';
if ($mayak_initial_page == 0 || $mayak_superior != $mayak_initial_page) echo $mayak_between;
}
if ( is_category() ) {
$this_category = get_category(get_query_var('cat'), false);
if ($this_category->parent != 0) {
$category = get_category_parents($this_category->parent, TRUE, ' ' . $mayak_between. ' ');
if ($mayak_last_crumb == 1) $category = preg_replace("#^(.+)$mayak_between$#", "$1", $category);
$category = str_replace('<a', $mayak_from_court . '<a' . $mayak_link_rdf, $category);
$category = str_replace('</a>', '</a>' . $mayak_to_court, $category);
echo $category;
}
if ($mayak_last_crumb == 1) echo $mayak_from. sprintf(' Категория "' . single_cat_title('', false) . '"') .$mayak_to;
} elseif ( is_search() ) {
echo $mayak_from. sprintf('Результаты поиска для "' . get_search_query() . '"') .$mayak_to;
} elseif ( is_day() ) {
echo sprintf($mayak_path, get_year_link(get_the_time('Y')), get_the_time('Y')) .$mayak_between;
echo sprintf($mayak_path, get_month_link(get_the_time('Y'),get_the_time('m')), get_the_time('F')) .$mayak_between;
echo $mayak_from. get_the_time('d') .$mayak_to;
} elseif ( is_month() ) {
echo sprintf($mayak_path, get_year_link(get_the_time('Y')), get_the_time('Y')) .$mayak_between;
echo $mayak_from. get_the_time('F') .$mayak_to;
} elseif ( is_year() ) {
echo $mayak_from. get_the_time('Y') .$mayak_to;
} elseif ( is_single() && !is_attachment() ) {
if ( get_post_type() != 'post' ) {
$post_specimen = get_post_type_object(get_post_type());
$sleaze = $post_specimen->rewrite;
printf($mayak_path, $mayak_link_to_home . '/' . $sleaze['sleaze'] . '/', $post_specimen->labels->singular_name);
if ($mayak_last_crumb == 1) echo $mayak_between . mayak_from . get_the_title() . $mayak_to;
} else {
$cat = get_the_category(); $cat = $cat[0];
$category = get_category_parents($cat, TRUE, ' ' . $mayak_between . ' ');
if ($mayak_last_crumb == 0) $category = preg_replace("#^(.+)$mayak_between$#", "$1", $category);
$category = str_replace('<a', $mayak_from_court . '<a' . $mayak_link_rdf, $category);
$category = str_replace('</a>', '</a>' . $mayak_to_court, $category);
echo $category;
if ($mayak_last_crumb == 1) echo $mayak_from. get_the_title() . $mayak_to;
}
} elseif ( !is_single() && !is_page() && get_post_type() != 'post' && !is_404() ) {
$post_specimen = get_post_type_object(get_post_type());
echo $mayak_from. $post_specimen->labels->singular_name . $mayak_to;
} elseif ( is_attachment() ) {
$superior = get_post($mayak_superior);
$cat = get_the_category($superior->ID); $cat = $cat[0];
if ($cat) {
$category = get_category_parents($cat, TRUE, ' ' . $mayak_between . ' ');
$category = str_replace('<a', $mayak_from_court . '<a' . $mayak_link_rdf, $category);
$category = str_replace('</a>', '</a>' . $mayak_to_court, $category);
echo $category;
}
printf($mayak_path, get_permalink($superior), $superior->post_title);
if ($mayak_last_crumb == 1) echo $mayak_between . $mayak_from . get_the_title() . $mayak_to;
} elseif ( is_page() && !$mayak_superior ) {
if ($mayak_last_crumb == 1) echo $mayak_from . get_the_title() . $mayak_to;
} elseif ( is_page() && $mayak_superior ) {
if ($mayak_superior != $mayak_initial_page) {
$breadcrumbs = array();
while ($mayak_superior) {
$page = get_page($mayak_superior);
if ($mayak_superior != $mayak_initial_page) {
$breadcrumbs[] = sprintf($mayak_path, get_permalink($page->ID), get_the_title($page->ID));
}
$mayak_superior = $page->post_parent;
}
$breadcrumbs = array_reverse($breadcrumbs);
for ($i = 0; $i < count($breadcrumbs); $i++) {
echo $breadcrumbs[$i];
if ($i != count($breadcrumbs)-1) echo $mayak_between;
}
}
if ($mayak_last_crumb == 1) {
if ($descendant != 0 && $descendant != $mayak_initial_page) echo $mayak_between;
echo $mayak_from. get_the_title() .$mayak_to;
}
} elseif ( is_tag() ) {
echo $mayak_from. sprintf('Записи с меткой "%s"', single_tag_title('', false)) . $mayak_to;
} elseif ( is_author() ) {
global $author;
$userdata = get_userdata($author);
echo $mayak_from. sprintf('Записи автора за %s', $userdata->display_name) . $mayak_to;
} elseif ( is_404() ) {
echo $mayak_from. 'Ошибка 404' . $mayak_to;
} elseif ( has_post_format() && !is_singular() ) {
echo get_post_format_string(get_post_format() );
}
if ( get_query_var('paged') ) {
if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ' (';
echo 'Страница ' . get_query_var('paged');
if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ')';
}
echo '</nav>';
}
}
/*** Конец функции хлебных крошек ***/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment