// ========================================================================= // Schema Markup (Video + FAQ) for Product Pages // ========================================================================= function add_custom_schema_to_head() { if (is_page()) { $allowed_templates = array('indoor-led-display.php', 'list-series.php'); $current_template = get_page_template_slug(); if (in_array($current_template, $allowed_templates, true)) { global $post; $post_id = $post->ID; if (have_rows('list_video', $post_id)) { $video_count = 0; while (have_rows('list_video', $post_id) && $video_count < 5) { the_row(); if (have_rows('youtube-body')) { while (have_rows('youtube-body')) { the_row(); $youtube_id = get_sub_field('youtube-number'); if ($youtube_id) { $video_title = get_sub_field('youtube-title') ?: get_sub_field('video-title', $post_id); $video_desc = get_sub_field('video-body', $post_id) ?: 'Professional LED display solutions'; $upload_date = get_post_meta($post_id, 'video_upload_date', true) ?: get_the_date('c', $post_id); $video_schema = array( '@context' => 'https://schema.org', '@type' => 'VideoObject', 'name' => sanitize_text_field($video_title), 'description' => sanitize_text_field(wp_trim_words($video_desc, 30, '')), 'thumbnailUrl' => array( "https://img.youtube.com/vi/{$youtube_id}/maxresdefault.jpg", "https://img.youtube.com/vi/{$youtube_id}/hqdefault.jpg" ), 'uploadDate' => $upload_date, 'contentUrl' => "https://www.youtube.com/watch?v={$youtube_id}", 'embedUrl' => "https://www.youtube.com/embed/{$youtube_id}", 'duration' => 'PT1M30S', 'interactionCount' => '1500' ); echo ''; $video_count++; } } } } } $qas = get_field('common_qa', $post_id); if ($qas && is_array($qas)) { $faq_items = array(); $unique_questions = array(); foreach (array_slice($qas, 0, 10) as $faq_post) { $question = get_the_title($faq_post); // FIX: FAQ 答案与页面可见
完全同源,统一取 get_the_excerpt(), // 杜绝 schema 与可见文本长度不一致(原 wp_trim_words 长文分支已移除) $answer = get_the_excerpt($faq_post); $clean_question = sanitize_text_field(strip_shortcodes($question)); $clean_answer = sanitize_text_field(strip_shortcodes($answer)); // FIX: 去掉 strlen < 50 过滤,避免丢弃短答案导致 schema 条数与可见不一致 if ( empty($clean_question) || empty($clean_answer) || in_array(md5($clean_question), $unique_questions, true) ) { continue; } $unique_questions[] = md5($clean_question); $faq_items[] = array( '@type' => 'Question', 'name' => $clean_question, 'acceptedAnswer' => array( '@type' => 'Answer', 'text' => $clean_answer ) ); } if (!empty($faq_items)) { $faq_schema = array( '@context' => 'https://schema.org', '@type' => 'FAQPage', 'mainEntity' => $faq_items ); echo ''; } } } } }