/* ----------------------------------------------------------------
* CATEGORY ARCHIVE: Structured Data
* ---------------------------------------------------------------*/
public function print_structured_data_category(){
if (!is_category()) return;
$term = get_queried_object();
if (!$term || is_wp_error($term)) return;
$opts = $this->get_opts();
$schemas = [];
// 1) CollectionPage cho trang category
$schemas[] = $this->build_collectionpage_schema($term, $opts);
// 2) BreadcrumbList cho category
$catBreadcrumb = $this->build_category_breadcrumb_schema($term);
if (!empty($catBreadcrumb)) $schemas[] = $catBreadcrumb;
// 3) Organization
$org = $this->build_organization_schema($opts);
if (!empty($org)) $schemas[] = $org;
// 4) LocalBusiness (nếu bật)
if (!empty($opts['lb_enabled'])) {
$lb = $this->build_localbusiness_schema($opts);
if (!empty($lb)) $schemas[] = $lb;
}
// 5) ItemList các bài trên trang (theo trang hiện tại)
$itemList = $this->build_category_itemlist_schema($term);
if (!empty($itemList)) $schemas[] = $itemList;
// In ra
foreach($schemas as $s){
if (empty($s)) continue;
$json = wp_json_encode($s, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
if ($json) {
echo ''."\n";
}
}
}
/**
* Schema cho CollectionPage (trang liệt kê bài viết theo chuyên mục)
*/
private function build_collectionpage_schema($term, $opts){
$name = single_term_title('', false);
$desc = term_description($term, 'category'); // HTML
$url = get_term_link($term);
// Lấy ngày chỉnh sửa gần nhất trong category (nếu có)
$lastMod = '';
$recent = get_posts([
'post_type' => 'post',
'posts_per_page' => 1,
'orderby' => 'modified',
'order' => 'DESC',
'tax_query' => [[
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => $term->term_id,
]],
'fields' => 'ids',
'no_found_rows' => true,
]);
if (!empty($recent)) {
$lastMod = get_the_modified_date('c', $recent[0]);
}
$siteName = !empty($opts['org_name']) ? $opts['org_name'] : get_bloginfo('name');
$siteUrl = !empty($opts['org_url']) ? $opts['org_url'] : site_url('/');
$data = [
"@context" => "https://schema.org",
"@type" => ["CollectionPage", "WebPage"],
"name" => wp_strip_all_tags($name),
"description" => wp_strip_all_tags($desc),
"url" => $url,
"isPartOf" => [
"@type" => "WebSite",
"name" => $siteName,
"url" => $siteUrl
],
"inLanguage" => get_bloginfo('language') ?: 'vi-VN',
"mainEntity" => [
"@type" => "ItemList",
"itemListElement" => [] // sẽ có thêm ở ItemList riêng bên dưới (để các crawler linh hoạt)
],
];
if ($lastMod) $data["dateModified"] = $lastMod;
return $data;
}
/**
* BreadcrumbList cho trang category
*/
private function build_category_breadcrumb_schema($term){
$items = [
[
"@type" => "ListItem",
"position" => 1,
"name" => "Trang chủ",
"item" => home_url('/')
],
[
"@type" => "ListItem",
"position" => 2,
"name" => wp_strip_all_tags($term->name),
"item" => get_term_link($term)
],
];
return [
"@context" => "https://schema.org",
"@type" => "BreadcrumbList",
"itemListElement" => $items
];
}
/**
* ItemList các bài viết xuất hiện trên trang category (trang hiện tại)
* - Giới hạn tối đa 20 để JSON gọn nhẹ
*/
private function build_category_itemlist_schema($term){
global $wp_query;
// Lấy danh sách post theo truy vấn trang hiện tại (đã có phân trang)
$posts = !empty($wp_query->posts) ? $wp_query->posts : [];
if (empty($posts)) return [];
$elements = [];
$i = 1;
foreach($posts as $p){
if ($i > 20) break; // giới hạn an toàn
$elements[] = [
"@type" => "ListItem",
"position" => $i,
"url" => get_permalink($p),
"name" => wp_strip_all_tags(get_the_title($p)),
];
$i++;
}
return [
"@context" => "https://schema.org",
"@type" => "ItemList",
"name" => wp_strip_all_tags($term->name),
"itemListElement" => $elements
];
}
https://bluecons.net/post-sitemap.xml
2025-09-22T08:17:46+00:00
https://bluecons.net/page-sitemap.xml
2025-08-22T06:39:27+00:00
https://bluecons.net/category-sitemap.xml
2025-09-22T08:17:46+00:00
https://bluecons.net/author-sitemap.xml
2025-09-23T16:24:38+00:00