-1

So I'm developing a theme and would like to implement an accordion for some client reviews. I followed this handy guide (and updated it for Bootstrap 4) which gave me this code:

<?php
    $args = array(
      'post_type' => 'review'
    );
    $the_query = new WP_Query($args);
  ?>

    <div class="row mb-5">
      <div class="col-md-9">

        <?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : ?>
        <?php $the_query->the_post(); ?>

        <div id="accordion" role="tablist" aria-multiselectable="true">
          <div class="card">
            <div class="card-header" role="tab" id="heading<?php the_ID() ?>">
              <h5 class="mb-0">
                <a data-toggle="collapse" data-parent="#accordion" href="#collapse<?php the_ID() ?>" aria-expanded="true" aria-controls="collapse<?php the_ID() ?>"><?php the_title(); ?></a>
              </h5>
            </div>

            <?php rewind_posts(); ?>

            <div id="collapse<?php the_ID() ?>" class="collapse <?php echo ($the_query->current_post == 0 ? 'show' : ''); ?>" role="tabpanel" aria-labelledby="heading<?php the_ID() ?>">
              <div class="card-block">
                <span><?php the_content(); ?></span>
              </div>
            </div>
          </div>
        </div>

        <?php endwhile; endif; ?>

      </div>
    </div>

    <?php wp_reset_query(); ?>

My issue is that the accordion function works, but only on the first click. All accordion tabs remain open after being clicked after that point, instead of closing when a new one is clicked. What should I do?

2
  • Hi, and welcome here. Even if your issue happens in the context of WordPress, it is an issue related to bootstrap and not to WordPress, os it is off-topic here. You should be more lucky asking on Stack Overflow.
    – gmazzap
    May 4, 2017 at 14:40
  • Hi! Thank you! I believe my question was geared much more to using the WordPress loop, but I appreciate the tip.
    – KreigD
    May 4, 2017 at 16:06

1 Answer 1

0

You put the accordion inside the loop, that's why it is not working as you want.

Try this:

<?php
    $args = array(
      'post_type' => 'review'
    );
    $the_query = new WP_Query($args);
  ?>

    <div class="row mb-5">
      <div class="col-md-9">
 <div id="accordion" role="tablist" aria-multiselectable="true">
        <?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : ?>
        <?php $the_query->the_post(); ?>


          <div class="card">
            <div class="card-header" role="tab" id="heading<?php the_ID() ?>">
              <h5 class="mb-0">
                <a data-toggle="collapse" data-parent="#accordion" href="#collapse<?php the_ID() ?>" aria-expanded="true" aria-controls="collapse<?php the_ID() ?>"><?php the_title(); ?></a>
              </h5>
            </div>

            <?php rewind_posts(); ?>

            <div id="collapse<?php the_ID() ?>" class="collapse <?php echo ($the_query->current_post == 0 ? 'show' : ''); ?>" role="tabpanel" aria-labelledby="heading<?php the_ID() ?>">
              <div class="card-block">
                <span><?php the_content(); ?></span>
              </div>
            </div>
          </div>


        <?php endwhile; endif; ?>
</div>
      </div>
    </div>

    <?php wp_reset_query(); ?>
4
  • That's not quite what I needed. My accordion is working beside the fact that once you click on a new tab, the previous tab stays open when it's supposed to close. You can click it again and collapse it, but it should collapse itself.
    – KreigD
    Apr 29, 2017 at 5:02
  • try this, it is same as your's one with a little tweak
    – BlueSuiter
    Apr 29, 2017 at 5:05
  • hmmm what is the answer? please explain what was the problem so other people can actually learn something from it Apr 29, 2017 at 5:45
  • Check the updated answer
    – BlueSuiter
    Apr 29, 2017 at 5:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.