Hey,
I want to add an accordion section to the blog post as a block.
website URL: happyoff.com
thank you so much for your attention and participation.
Hey,
I want to add an accordion section to the blog post as a block.
website URL: happyoff.com
thank you so much for your attention and participation.
To add an accordion section to your blog post as a block on your Shopify store, you can create a custom Liquid block or use custom code with JavaScript and CSS. Here’s a step-by-step guide:
Create a new section file (e.g., accordion-section.liquid):
{% for block in section.blocks %}
{{ block.content }}
{% endfor %}
{% schema %}
{
"name": "Accordion Section",
"blocks": [
{
"type": "accordion_item",
"name": "Accordion Item",
"settings": [
{
"id": "title",
"type": "text",
"label": "Title"
}
]
}
],
"presets": [
{
"name": "Accordion Section",
"category": "Blog"
}
]
}
{% endschema %}
Add the following CSS to your theme’s theme.scss.liquid or base.css file:
.accordion {
border: 1px solid #ccc;
}
.accordion-header {
background-color: #f1f1f1;
cursor: pointer;
padding: 10px;
border: none;
outline: none;
width: 100%;
text-align: left;
}
.accordion-content {
display: none;
padding: 10px;
background-color: white;
border-top: 1px solid #ccc;
}
.accordion-header.active + .accordion-content {
display: block;
}
Insert this JavaScript code before the closing tag in theme.liquid
document.querySelectorAll('.accordion-header').forEach(button => {
button.addEventListener('click', () => {
button.classList.toggle('active');
const content = button.nextElementSibling;
if (content.style.display === 'block') {
content.style.display = 'none';
} else {
content.style.display = 'block';
}
});
});
This should give you a functional accordion section on your blog post where you can add content as collapsible panels. Let me know if you need help with further customization or specific tweaks!
Hi @Optimist1
You can add an accordion on each it can be done using liquid customization
Create Metafields for Blog Posts:
Add Liquid Code:
Go to Online Store > Themes > Edit Code.
Open the article.liquid or blog.liquid template.
Insert the following Liquid code to create an accordion structure based on your metafields:
{% if article.metafields.accordion.title and article.metafields.accordion.content %}
{{ article.metafields.accordion.content }}
{% endif %}
.accordion .accordion-item {
border-bottom: 1px solid #ddd;
}
.accordion-header {
background-color: #f1f1f1;
cursor: pointer;
padding: 15px;
width: 100%;
text-align: left;
border: none;
outline: none;
font-size: 16px;
transition: background-color 0.3s ease;
}
.accordion-header:hover {
background-color: #ddd;
}
.accordion-content {
display: none;
padding: 15px;
background-color: #fafafa;
}
.accordion-content p {
margin: 0;
}
document.addEventListener("DOMContentLoaded", function() {
var accHeaders = document.querySelectorAll(".accordion-header");
accHeaders.forEach(function(header) {
header.addEventListener("click", function() {
// Toggle active class
this.classList.toggle("active");
// Toggle content display
var content = this.nextElementSibling;
if (content.style.display === "block") {
content.style.display = "none";
} else {
content.style.display = "block";
}
});
});
});
I Hope this Works Mostly for this kind of work you need an Expert but if you can do that then I that is perfect Hope this work
If This Solution Word Kindly Like this And Mark it as a Solution or Buy Me a Coffee
Thanks, But These codes add it as a section, I want to add it block to integrate it into my blog post
Please mark a solution then I will share a code shortly.
I want to add each blog post with its FAQs and not make a general FAQ.
The above code do the same it create a meta field that mean you get faqs for each blogs