Dynamic Accordion in WordPress I used the Bootstrap v4.0.0 accordion to display my business services. Further, I created a custom post type &quot;service&quot; 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:
&lt;div class=&quot;wv-archive-container wv-boxed-wrapper&quot;&gt;
    &lt;div class=&quot;container&quot;&gt;
        &lt;div class=&quot;row&quot;&gt;
            &lt;div class=&quot;col-sm&quot;&gt;
                &lt;h1 id=&quot;wv-archive-header-h1&quot;&gt;All Services&lt;/h1&gt;
                &lt;div id=&quot;wv-accordion&quot; class=&quot;wv-accordion&quot;&gt;
                    &lt;div class=&quot;card mb-0&quot;&gt;

                        &lt;?php $wv_services = new WP_Query( array(
                            'posts_per_page' =&gt; -1,
                            'post_type'      =&gt; 'service',
                            'orderby'        =&gt; 'date',
                            'order'          =&gt; 'ASC'
                        ) );

                        while ( $wv_services-&gt;have_posts() ) {

                            $enum_of_collapsible_str_numbers = [
                                &quot;One&quot;,
                                &quot;Two&quot;,
                                &quot;Three&quot;,
                                &quot;Four&quot;,
                                &quot;Five&quot;,
                                &quot;Six&quot;,
                                &quot;Seven&quot;,
                                &quot;Eight&quot;,
                                &quot;Nine&quot;,
                                &quot;Ten&quot;,
                                &quot;Eleven&quot;,
                                &quot;Twelve&quot;,
                                &quot;Thirtenn&quot;,
                                &quot;Fourteen&quot;,
                                &quot;Fivteen&quot;,
                                &quot;Sixteen&quot;,
                                &quot;Seventeen&quot;,
                                &quot;Eighteen&quot;,
                                &quot;Nineteen&quot;,
                                &quot;Twenty&quot;
                            ];
                            $total_number_of_service_posts = wp_count_posts('service')-&gt;publish;
                            for ($collapsible_str_number = 0; $collapsible_str_number &lt; $total_number_of_service_posts; $collapsible_str_number++) {

                                $wv_services-&gt;the_post(); ?&gt;

                                &lt;div class=&quot;card-header collapsed&quot; style=&quot;cursor:pointer;&quot; data-toggle=&quot;collapse&quot;
                                     href=&quot;#collapse&lt;?php echo $enum_of_collapsible_str_numbers[$collapsible_str_number] ?&gt;&quot; rel=”nofollow”&gt;
                                    &lt;a class=&quot;card-title&quot;&gt;&lt;h2&gt;&lt;?php the_title(); ?&gt;&lt;/h2&gt;&lt;/a&gt;
                                &lt;/div&gt;
                                &lt;div id=&quot;collapse&lt;?php echo $enum_of_collapsible_str_numbers[$collapsible_str_number] ?&gt;&quot; class=&quot;card-body collapse&quot;
                                     data-parent=&quot;#accordion&quot;&gt;
                                    &lt;p&gt;&lt;?php the_content(); ?&gt;&lt;/p&gt;
                                &lt;/div&gt;

                            &lt;?php }
                        }
                        wp_reset_postdata();
                        ?&gt;

                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
Not sure what you are trying to achieve, but you can use numerical values for enum_of_collapsible_numbers and you can generate them like this
$enum_of_collapsible_numbers = range(1, 20);