Skip to content

Instantly share code, notes, and snippets.

@RiodeJaneiroo
Last active May 3, 2019 08:37
Show Gist options
  • Save RiodeJaneiroo/124a3475c1fcee21a5c061169af5d5ae to your computer and use it in GitHub Desktop.
Save RiodeJaneiroo/124a3475c1fcee21a5c061169af5d5ae to your computer and use it in GitHub Desktop.
[Wordrepss Breadcrumbs JSON-LD] JSON-LD breadcrumbs #php #seo #wordpress #breadcrumbs
<?php
$cur_cat = get_term_by('id', get_query_var('cat'), 'category');
?>
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "BreadcrumbList",
"itemListElement":
[
{
"@type": "ListItem",
"position": 1,
"item": {
"@id": "<?php echo get_term_link($cur_cat->term_id); ?>",
"name": "<?php echo $cur_cat->name; ?>"
}
}
]
}
</script>
<?php
$taxList = get_the_terms( get_the_ID(), 'product-category' );
?>
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "BreadcrumbList",
"itemListElement":
[
<?php
$count = 1;
foreach( $taxList as $category ):
?>
{
"@type": "ListItem",
"position": <?php echo $count;?>,
"item": {
"@id": "<?php echo get_term_link($category->term_id); ?>",
"name": "<?php echo $category->name; ?>"
}
},
<?php
$count++;
endforeach;
?>
{
"@type": "ListItem",
"position": <?php echo $count; ?>,
"item": {
"@id": "<?php the_permalink(get_the_ID());?>",
"name": "<?php the_title(); ?>"
}
}
]
}
</script>
<?php
$cur_term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy'));
$parentCat = get_term_by('id', $cur_term->parent, get_query_var('taxonomy'));
?>
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "BreadcrumbList",
"itemListElement":
[
{
"@type": "ListItem",
"position": 1,
"item": {
"@id": "<?php echo get_term_link($parentCat->term_id); ?>",
"name": "<?php echo $parentCat->name; ?>"
}
},{
"@type": "ListItem",
"position": 2,
"item": {
"@id": "<?php echo get_term_link($cur_term->term_id); ?>",
"name": "<?php echo $cur_term->name; ?>"
}
}
]
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment