-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuncode-faq-structured-data-script.js
42 lines (35 loc) · 1.21 KB
/
uncode-faq-structured-data-script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
jQuery(document).ready(function($) {
function generateFAQSchema() {
const $faqContainer = $('.uncode-accordion');
if ($faqContainer.length === 0) {
return;
}
const $questions = $faqContainer.find('.panel-title a span');
const $answers = $faqContainer.find('.panel-body .uncode_text_column');
let faqList = [];
$questions.each(function(index) {
// Extract all raw text from each answer
let answerText = $answers.eq(index).text().trim();
faqList.push({
"@type": "Question",
"name": $(this).text().trim(),
"acceptedAnswer": {
"@type": "Answer",
"text": answerText
}
});
});
const faqSchema = {
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": faqList
};
// Add JSON-LD to head
const $script = $('<script>')
.attr('type', 'application/ld+json')
.text(JSON.stringify(faqSchema));
$('head').append($script);
}
// Call function to generate JSON-LD
generateFAQSchema();
});