اسکیمای مسیر راهنما در وردپرس (BreadcrumbList Schema JSON-LD)
با تزریق ساختار `BreadcrumbList` در هوک `wp_head` برای نوشتههای تکی، سلسله مراتب کامل صفحه را به صورت JSON-LD به گوگل ارائه دهید.
<?php
/**
* تولید خودکار اسکیمای مسیر راهنما (BreadcrumbList) برای نوشتههای وبلاگ
*/
function bisco_output_breadcrumb_schema() {
if ( ! is_single() ) {
return;
}
global $post;
$breadcrumbs = array();
// گام اول: صفحه نخست
$breadcrumbs[] = array(
'@type' => 'ListItem',
'position' => 1,
'name' => 'صفحه اصلی',
'item' => esc_url( home_url( '/' ) ),
);
$position = 2;
// گام دوم: دسته اول نوشته
$categories = get_the_category( $post->ID );
if ( ! empty( $categories ) ) {
$first_cat = $categories[0];
$breadcrumbs[] = array(
'@type' => 'ListItem',
'position' => $position,
'name' => esc_html( $first_cat->name ),
'item' => esc_url( get_category_link( $first_cat->term_id ) ),
);
$position++;
}
// گام سوم: نوشته جاری
$breadcrumbs[] = array(
'@type' => 'ListItem',
'position' => $position,
'name' => esc_html( get_the_title( $post ) ),
'item' => esc_url( get_permalink( $post ) ),
);
$schema = array(
'@context' => 'https://schema.org',
'@type' => 'BreadcrumbList',
'itemListElement' => $breadcrumbs,
);
echo '<script type="application/ld+json">' . wp_json_encode( $schema, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ) . '</script>' . "
";
}
add_action( 'wp_head', 'bisco_output_breadcrumb_schema', 3 );