Last active
February 3, 2020 09:11
-
-
Save RiodeJaneiroo/09a4b63641110e72f6de3591dd9e275d to your computer and use it in GitHub Desktop.
[Разметка для хлебных крошек] #wordpress #seo #breadcrumbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function schemaBreadcrumbsJson() { | |
global $post; | |
$arrayBreadcrumbs = array(); | |
$parent_id = ( $post ) ? $post->post_parent : ''; | |
if(is_category()) { // для рубрики | |
$parents = get_ancestors( get_query_var('cat'), 'category' ); | |
foreach ( array_reverse( $parents ) as $cat ) { | |
array_push($arrayBreadcrumbs, [get_cat_name( $cat ), get_category_link( $cat )]); | |
} | |
$cat = get_query_var('cat'); | |
array_push($arrayBreadcrumbs, [get_cat_name( $cat ), get_category_link( $cat )]); | |
} | |
else if (is_single()) { // для одиночной записи | |
if ( get_post_type() != 'post' ) { // для кастомного типа записи | |
$parents = array_reverse( wp_get_object_terms($post->ID, get_taxonomies()) ); | |
foreach ( $parents as $cat ) { | |
array_push($arrayBreadcrumbs, [$cat->name, get_term_link($cat->term_id, $cat->taxonomy)]); | |
} | |
} else { // для обычного поста | |
$cat = get_the_category(); $catID = $cat[0]->cat_ID; | |
$parents = get_ancestors( $catID, 'category' ); | |
$parents = array_reverse( $parents ); | |
array_push( $parents, $catID); | |
foreach ( $parents as $cat ) { | |
array_push($arrayBreadcrumbs, [get_cat_name( $cat ), get_category_link( $cat )]); | |
} | |
} | |
// add current post | |
$postTitle = get_the_title(); | |
$postLink = get_the_permalink(); | |
array_push($arrayBreadcrumbs, [$postTitle, $postLink]); | |
} | |
else if(is_archive()) { | |
$term = get_queried_object(); | |
// add current tax | |
$titleArchive = $term->name; | |
$linkArchive = get_term_link($term->term_id, $term->taxonomy); | |
array_push($arrayBreadcrumbs, [$titleArchive, $linkArchive]); | |
} elseif ( is_page() ) { // для страниц | |
if($parent_id) { | |
$parents = get_post_ancestors( get_the_ID() ); | |
foreach ( array_reverse( $parents ) as $pageID ) { | |
array_push($arrayBreadcrumbs, [get_the_title( $pageID ), get_page_link( $pageID )]); | |
} | |
} | |
// add current page | |
$postTitle = get_the_title(); | |
$postLink = get_the_permalink(); | |
array_push($arrayBreadcrumbs, [$postTitle, $postLink]); | |
} | |
if ($arrayBreadcrumbs) : ?> | |
<script type="application/ld+json"> | |
{ | |
"@context": "http://schema.org", | |
"@type": "BreadcrumbList", | |
"itemListElement": [ | |
<?php | |
$count = 1; | |
foreach ($arrayBreadcrumbs as $item) : | |
if ($count != 1) echo ','; | |
?> { | |
"@type": "ListItem", | |
"position": <?php echo $count; ?>, | |
"item": { | |
"@id": "<?php echo $item[1]; ?>", | |
"name": "<?php echo $item[0]; ?>" | |
} | |
} | |
<?php | |
$count++; | |
endforeach; | |
?> | |
] | |
} | |
</script> | |
<?php | |
endif; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function schemaBreadcrumbsJson() { | |
global $post; | |
$arrayBreadcrumbs = array(); | |
$parent_id = ( $post ) ? $post->post_parent : ''; | |
if(is_category()) { // для рубрики | |
$parents = get_ancestors( get_query_var('cat'), 'category' ); | |
foreach ( array_reverse( $parents ) as $cat ) { | |
array_push($arrayBreadcrumbs, [get_cat_name( $cat ), get_category_link( $cat )]); | |
} | |
$cat = get_query_var('cat'); | |
array_push($arrayBreadcrumbs, [get_cat_name( $cat ), get_category_link( $cat )]); | |
} | |
else if (is_single()) { // для одиночной записи | |
if ( get_post_type() != 'post' ) { // для кастомного типа записи | |
$parents = array_reverse( wp_get_object_terms($post->ID, get_taxonomies()) ); | |
if($parents){ | |
foreach ( $parents as $cat ) { | |
array_push($arrayBreadcrumbs, [$cat->name, get_term_link($cat->term_id, $cat->taxonomy)]); | |
} | |
} | |
if($parent_id) { | |
$parents = get_post_ancestors( get_the_ID() ); | |
foreach ( array_reverse( $parents ) as $pageID ) { | |
array_push($arrayBreadcrumbs, [get_the_title( $pageID ), get_page_link( $pageID )]); | |
} | |
} | |
} else { // для обычного поста | |
$cat = get_the_category(); $catID = $cat[0]->cat_ID; | |
$parents = get_ancestors( $catID, 'category' ); | |
$parents = array_reverse( $parents ); | |
array_push( $parents, $catID); | |
foreach ( $parents as $cat ) { | |
array_push($arrayBreadcrumbs, [get_cat_name( $cat ), get_category_link( $cat )]); | |
} | |
} | |
// add current post | |
$postTitle = get_the_title(); | |
$postLink = get_the_permalink(); | |
array_push($arrayBreadcrumbs, [$postTitle, $postLink]); | |
} | |
else if(is_archive()) { | |
$term = get_queried_object(); | |
// add current tax | |
$titleArchive = $term->name; | |
$linkArchive = get_term_link($term->term_id, $term->taxonomy); | |
array_push($arrayBreadcrumbs, [$titleArchive, $linkArchive]); | |
} elseif ( is_page() ) { // для страниц | |
if($parent_id) { | |
$parents = get_post_ancestors( get_the_ID() ); | |
foreach ( array_reverse( $parents ) as $pageID ) { | |
array_push($arrayBreadcrumbs, [get_the_title( $pageID ), get_page_link( $pageID )]); | |
} | |
} | |
// add current page | |
$postTitle = get_the_title(); | |
$postLink = get_the_permalink(); | |
array_push($arrayBreadcrumbs, [$postTitle, $postLink]); | |
} | |
if ($arrayBreadcrumbs) : ?> | |
<script type="application/ld+json"> | |
{ | |
"@context": "http://schema.org", | |
"@type": "BreadcrumbList", | |
"itemListElement": [ | |
<?php | |
$count = 1; | |
foreach ($arrayBreadcrumbs as $item) : | |
if ($count != 1) echo ','; | |
?> { | |
"@type": "ListItem", | |
"position": <?php echo $count; ?>, | |
"item": { | |
"@id": "<?php echo $item[1]; ?>", | |
"name": "<?php echo $item[0]; ?>" | |
} | |
} | |
<?php | |
$count++; | |
endforeach; | |
?> | |
] | |
} | |
</script> | |
<?php | |
endif; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment