Hide Heading if ACF Field is empty

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?

Topic php custom-field theme-development Wordpress

Category Web


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; ?>

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.