BreadcrumbList – dane uporządkowane

BreadcrumbList – dane uporządkowane

Dodaje do strony dane uporządkowane breadcrumbs dla Wordpressa.

function knight_schema_breadcrumbs() {

global $post,$wp_query;
if ( !is_front_page() ) {

$position1='';
$position2='';
$position3='';
$position4='';

$position1 ='{"@type": "ListItem","position": 1,"name": "Home","item": "'.get_home_url() .'" },';

if ( is_archive() && !is_tax() && !is_category() && !is_tag() && !is_author() ) {


$position2 ='{"@type": "ListItem","position": 2,"name": "'.post_type_archive_title($prefix, false).'"}';

}
else if ( is_archive() && is_tax() && !is_category() && !is_tag() ) {


$post_type = get_post_type();


if($post_type != 'post') {



$post_type_object = get_post_type_object($post_type);
$post_type_archive = get_post_type_archive_link($post_type);


$position2 ='{"@type": "ListItem","position": 2,"name": "'.$post_type_object->labels->name.'","item": "'.$post_type_archive.'" },';
}

$custom_tax_name = get_queried_object()->name;

$position3='{"@type": "ListItem","position": 3,"name": "'.$custom_tax_name.'"}';
}
else if ( is_single() ) {


$post_type = get_post_type();


if($post_type != 'post') {

$post_type_object = get_post_type_object($post_type);
$post_type_archive = get_post_type_archive_link($post_type);

 

if($post_type == 'kblog'){$post_type_archive = 'https://teamjj.pl/blog';}

$position2 ='{"@type": "ListItem","position": 2,"name": "'.$post_type_object->labels->name.'","item": "'.$post_type_archive.'" },';

//custom post blog
if($post_type == 'kblog'){$custom_taxonomy = 'blogcategory';}

}


$category = get_the_category();
$categories2 = get_the_category();
if(!empty($category)) {


$last_category = end(array_values($category));


$get_cat_parents = rtrim(get_category_parents($last_category->term_id, true, ','),',');
$cat_parents = explode(',',$get_cat_parents);



foreach( $categories2 as $category ) {


$position2 ='{"@type": "ListItem","position": 2,"name": "'.$category->name.'","item": "'.get_category_link( $category->term_id ).'" },';
}

}


$taxonomy_exists = taxonomy_exists($custom_taxonomy);
if(empty($last_category) && !empty($custom_taxonomy) && $taxonomy_exists) {

$taxonomy_terms = get_the_terms( $post->ID, $custom_taxonomy );
$cat_id = $taxonomy_terms[0]->term_id;
$cat_nicename = $taxonomy_terms[0]->slug;
$cat_link = get_term_link($taxonomy_terms[0]->term_id, $custom_taxonomy);
$cat_name = $taxonomy_terms[0]->name;

}


if(!empty($last_category)) {


$position3='{"@type": "ListItem","position": 3,"name": "'.get_the_title().'"}';


} else if(!empty($cat_id)) {


$position3 ='{"@type": "ListItem","position": 3,"name": "'.$cat_name.'","item": "'.$cat_link .'" },';
$position4='{"@type": "ListItem","position": 4,"name": "'.get_the_title().'"}';

} else {


$position3='{"@type": "ListItem","position": 3,"name": "'.get_the_title().'"}';

}

}
else if ( is_category() ) {


$position2 ='{"@type": "ListItem","position": 2,"name": "'.single_cat_title('', false) .'"}';
}
else if ( is_page() ) {


if( $post->post_parent ){


$anc = get_post_ancestors( $post->ID );


$anc = array_reverse($anc);


if ( !isset( $parents ) ) $parents = null;
foreach ( $anc as $ancestor ) {

$position2 ='{"@type": "ListItem","position": 2,"name": "'.get_the_title($ancestor).'","item": "'.get_permalink($ancestor) .'" },';
}





$position3 ='{"@type": "ListItem","position": 3,"name": "'.get_the_title().'"}';

} else {


$position2 ='{"@type": "ListItem","position": 2,"name": "'.get_the_title().'"}';

}

}

else if ( is_author() ) {


global $author;
$userdata = get_userdata( $author );


$position2 ='{"@type": "ListItem","position": 2,"name": "'.$userdata->display_name.'"}';

}

 

$out .= '<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "BreadcrumbList",
"itemListElement": ['.$position1 . $position2 . $position3 . $position4.']
}
</script>';
echo $out;

}
}
add_action('wp_head', 'knight_schema_breadcrumbs');

Jak przydatny był ten post?

Kliknij gwiazdkę, aby ją ocenić!

Średnia ocena 5 / 5. Liczba głosów: 4

Jak dotąd brak głosów! Oceń ten post jako pierwszy.

Więcej optymalizacji w kategorii wordpress
Schema Article