I used the Bootstrap v4.0.0 accordion to display my business services. Further, I created a custom post type "service" for my services (Each service is a post).
To display my services dynamically and not hardcode them, I implemented a for-loop within the WordPress while-loop which uses an enumeration to provide the number of the collapsible.
I don't like my solution, especially the enumeration - Is there a better way to solve this? If you see any other improvement, I would be glad to hear also.
See my code below:
<div class="wv-archive-container wv-boxed-wrapper">
<div class="container">
<div class="row">
<div class="col-sm">
<h1 id="wv-archive-header-h1">All Services</h1>
<div id="wv-accordion" class="wv-accordion">
<div class="card mb-0">
<?php $wv_services = new WP_Query( array(
'posts_per_page' => -1,
'post_type' => 'service',
'orderby' => 'date',
'order' => 'ASC'
) );
while ( $wv_services->have_posts() ) {
$enum_of_collapsible_str_numbers = [
"One",
"Two",
"Three",
"Four",
"Five",
"Six",
"Seven",
"Eight",
"Nine",
"Ten",
"Eleven",
"Twelve",
"Thirtenn",
"Fourteen",
"Fivteen",
"Sixteen",
"Seventeen",
"Eighteen",
"Nineteen",
"Twenty"
];
$total_number_of_service_posts = wp_count_posts('service')->publish;
for ($collapsible_str_number = 0; $collapsible_str_number < $total_number_of_service_posts; $collapsible_str_number++) {
$wv_services->the_post(); ?>
<div class="card-header collapsed" style="cursor:pointer;" data-toggle="collapse"
href="#collapse<?php echo $enum_of_collapsible_str_numbers[$collapsible_str_number] ?>" rel=”nofollow”>
<a class="card-title"><h2><?php the_title(); ?></h2></a>
</div>
<div id="collapse<?php echo $enum_of_collapsible_str_numbers[$collapsible_str_number] ?>" class="card-body collapse"
data-parent="#accordion">
<p><?php the_content(); ?></p>
</div>
<?php }
}
wp_reset_postdata();
?>
</div>
</div>
</div>
</div>
</div>
</div>