<!DOCTYPE html>
<html>
<head>
<title>Convert Yoast SEO FAQ Block into an Accordion with Collapsible Headers</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<div class="schema-faq">
<div class="schema-faq-section" id="faq-question-1687650041365">
<strong class="schema-faq-question">What is the Yoast SEO FAQ Block?</strong>
<p class="schema-faq-answer">
The Yoast SEO FAQ Block is a feature provided by the Yoast SEO plugin, which is a popular SEO plugin for WordPress websites. The FAQ Block is a Gutenberg block that allows you to easily create and add FAQ content to your WordPress pages or posts.
With the Yoast SEO plugin installed and activated, you can utilize the FAQ Block to create structured FAQ content that is optimized for search engines. The block provides a user-friendly interface where you can add questions and corresponding answers.
</p>
</div>
<div class="schema-faq-section" id="faq-question-1687650053029">
<strong class="schema-faq-question">How do I convert the Yoast SEO FAQ Block to an accordion?</strong>
<p class="schema-faq-answer">
To change the default Yoast SEO FAQ Block into an accordion, you'll need to apply custom CSS and JavaScript to your theme. The following styles define the visual presentation of the Yoast SEO FAQ Block on the frontend, including the appearance of questions, the toggle functionality of expanding and collapsing answers, and specific styles for the block editor environment.
</p>
</div>
</div>
</body>
</html>
/* Frontend Styles */
.schema-faq-section {
font-family: Arial;
background: white;
margin: 1rem 0;
border: 2px solid rgba(0, 0, 0, 0.13);
border-radius: 10px;
box-shadow: 2px 2px 6px 2px rgba(0, 0, 0, 0.03);
}
.schema-faq-question {
cursor: pointer;
display: flex;
align-items: center;
transition: opacity ease 0.25s;
padding: 1rem;
color: var(--wp--preset--color--primary);
font-size: var(--wp--preset--font-size--x-large);
}
.schema-faq-question:hover {
color: var(--wp--preset--color--secondary);
}
.schema-faq-question:after {
width: 16px;
height: 20px;
display: inline-block;
margin-left: auto;
margin-right: 5px;
vertical-align: top;
color: inherit;
content: "+";
}
.schema-faq-question.expanded:after {
content: "-";
}
.schema-faq-question:hover {
opacity: 0.75;
}
.schema-faq-answer {
padding: 0 1rem 1rem 1rem;
display: none;
}
.schema-faq-answer.default {
display: block;
}
/* Backend Styles: Ensure all content is visible in the block editor */
.editor-styles-wrapper .schema-faq-question {
cursor: text;
}
.editor-styles-wrapper .schema-faq-answer {
display: block;
}
jQuery(function ($) {
var yoast = {
accordion: function () {
var isAnimating = false; // Flag variable to track animation state
$(".schema-faq-section")
.find(".schema-faq-question")
.click(function (event) {
event.stopPropagation(); // Stop event propagation
if (isAnimating) {
return; // Ignore click if animation is in progress
}
isAnimating = true; // Set animation flag
var answer = $(this).nextAll(".schema-faq-answer").eq(0);
// Expand or collapse this panel
answer.slideToggle(250, function () {
$(this).toggleClass("expanded");
$(this).prev(".schema-faq-question").toggleClass("expanded"); // Toggle 'expanded' class on question element
isAnimating = false; // Reset animation flag after animation completes
});
// Hide the other panels
$(".schema-faq-answer").not(answer).slideUp("fast");
});
$(".schema-faq-section .schema-faq-question").click(function (event) {
event.stopPropagation(); // Stop event propagation
if (isAnimating) {
return; // Ignore click if animation is in progress
}
isAnimating = true; // Set animation flag
$(".schema-faq-section .schema-faq-question")
.not($(this))
.removeClass("expanded");
$(this).toggleClass("expanded");
isAnimating = false; // Reset animation flag
});
}
};
yoast.accordion();
});
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.