0

I am trying to add a "Local FAQ" section to all the individual location pages on a site with literally thousands of individual location pages. I am doing this by using ACF field in the custom post type. I've implemented this in a staging site. To give the local FAQ section a heading as opposed to there just being a list of questions & answers, I added a h3 as seen below

    <h3 class="local_faq" style="text-align: center; font-size: 50px;"><?php the_title(); ?> Dumpster Rental FAQs</h3>

However, since there are literally thousands of these pages, I want to implement this over time so I can still publish other changes I've made to the production site and therefore I don't want this h3 to be visible on pages that I haven't added any FAQs for yet.

Here's the full code for the local FAQ section:

<h3 class="local_faq" style="text-align: center; font-size: 50px;"><?php the_title(); ?> Dumpster Rental FAQs</h3>
<div class="local_faq">
    <?php if (get_field('local_faq')) : ?>
        <?php while (the_repeater_field('local_faq')) : ?>
            <div class="local_faq" style="margin-left: 100px; margin-right: 100px;">
                <h4>
                    <?php the_sub_field('question'); ?>
                </h4>
                <p>
                    <?php the_sub_field('answer'); ?>
                </p> 
            </div>
        <?php endwhile; ?>
    <?php endif; ?>
</div>

So, how do I hide the h3 if there aren't any FAQs entered yet?

1 Answer 1

1

Move the outer(of the two) div.local_faq inside the if statement which checks if the faq field is set, because that’s the outer parent container. Also it would make sense to change the outer container div to have a plural div.local_faqsclass instead of the same class as the nested div.local_faq

Then just add the h3 part inside the outer div.local_faqs and the faqs and header will only show if the field is set:

<?php if (get_field('local_faq')) : ?>
    <div class=“local_faqs”>
        <h3 class="local_faq" style="text-align: center; font-size: 50px;"><?php the_title(); ?> Dumpster Rental FAQs</h3>
        <?php while (the_repeater_field('local_faq')) : ?>
            <div class="local_faq" style="margin-left: 100px; margin-right: 100px;">
                <h4><?php the_sub_field('question'); ?></h4>
                <p><?php the_sub_field('answer'); ?></p> 
            </div>
        <?php endwhile; ?>
    </div>
<?php endif; ?>
2
  • 1
    Awesome! It works, I just had to add <?php endif; ?> that's the only thing that was missing from your code Apr 30 at 21:45
  • @JasonLevine edited answer to show the h3 part and the faq part together when the field is set. Apr 30 at 21:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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