Support

Account

Home Forums General Issues Fieldtype Checkbox only gets one *unselected* value

Solved

Fieldtype Checkbox only gets one *unselected* value

  • Hi

    I have a problem regarding a list that should show my services. But i can’t get the values out. I have tried to use the documentation and tried every single solution on that page..

    I can’t see if my code is missing anything or if i am using the fieldtype wrong.

    When i post my value out i only get returned one unselected list item.

    Here’s my code:

    
    <?php 
    $active_services = get_field("aktive_ydelser");
    
    //var_dump($active_services);
    
    if( $active_services ): 
    ?>
    <p>Samarbejdet indeholder bla.</p>
    <ul>
    <?php foreach ( $active_services as $active_service): ?>
    <li class="active-service"><?php echo $active_service; ?></li>
    <?php endforeach; ?>
    </ul>
    <?php endif; ?>
    

    I have attached a screenshot of my field settings.

    Hope you can help – my hairs are getting more grey.
    – Christopher

  • What type of page is this field on? Post? Term? Options? Something else?

    Where are you adding the code, need more context. What template are you adding it to? Is it inside any type of loop?

  • Hi John

    Its a standard page with a custom page template.

    Its inside a post loop so i can start the page with text from the editor, and then fill in the ACF fields to add items to the sections on the page like the previous example.

    
    <?php
    while ( have_posts() ) : the_post();
    
    $active_services = get_field("aktive_ydelser");
    
    //var_dump($active_services);
    
    if( $active_services ): 
    ?>
    <p>Samarbejdet indeholder bla.</p>
    <ul>
    <?php foreach ( $active_services as $active_service): ?>
    <li class="active-service"><?php echo $active_service; ?></li>
    <?php endforeach; ?>
    </ul>
    <?php endif; 
    endwhile;
    ?>
    

    Is that enough context or?

  • And if neccerary – This page is my frontpage.

  • I don’t really see what the issue could be to be honest.

    In your Settings => Reading settings are you what do you have set to show on the front page? Your latest posts OR A static Page?

  • And i have other fields next to this section that works. Sorry for the multiple answers (i will try to keep it to one next time)

  • The frontpage setting is set to a static page .

  • I don’t see any reason that this should not be working.

    The only thing that I can think of is that something else is happening before you get the checkbox field and show it that is somehow confusing ACF about what post to get the values from.

    Are there any relationship fields or post object fields used before this? Any other loop that might be altering the global $post value?

  • I only have that one loop, and then other fields like text and link

    I don’t know if you can see much out of it – there are php between the Danish mumbojumbo language but here is the whole page code:

    
    <?php
    
        /**
         * Template Name: Forside
         */
    
        get_header();
    
    ?>
    <main class="main" role="main">
        <div class="container">
    
            <?php while ( have_posts() ) : the_post(); ?>
    
                        <div class="content">
                            <?php the_content(); ?>
    
                            <?php 
                            $hero_cta = get_field('forside_hero_cta'); 
                            if ($hero_cta) :
                            ?>
                            <div class="focus_cta">
                                <a href="<?php echo $hero_cta['url']; ?>" title="<?php echo $hero_cta['title']; ?>" class="cta"><?php echo $hero_cta['title']; ?> <i class="fa-solid fa-briefcase-medical"></i></a>
                            </div>
                            <?php endif; ?>
    
                            <?php
                            $services = get_field('forside_ydelser');
                            if( $services ): ?>
                                <div class="services-section">    
                                    <h2><?php echo $services['ydelse_overskrift']; ?></h2> 
                                    <div class="services">
                                        <a href="<?php echo $services['cta_link_1']['url']; ?>" title="<?php echo $services['cta_link_1']['title']; ?>" class="service"> 
                                            <h3><?php echo $services['ydelse_1_overskrift']; ?> <i class="fa-solid fa-circle-nodes"></i></h3>
                                            <?php echo $services['ydelse_1_tekst']; ?>
                                            <span class="readmore"><?php echo $services['cta_link_1']['title']; ?> <i class="fa-solid fa-arrow-right-long"></i></span>
                                        </a>
    
                                        <a href="<?php echo $services['cta_link_2']['url']; ?>" title="<?php echo $services['cta_link_2']['title']; ?>" class="service">
                                            <h3><?php echo $services['ydelse_2_overskrift']; ?> <i class="fa-solid fa-gears"></i></h3>
                                            <?php echo $services['ydelse_2_tekst']; ?>
                                            <span class="readmore"><?php echo $services['cta_link_2']['title']; ?> <i class="fa-solid fa-arrow-right-long"></i></span>
                                        </a>
                                    
                                        <a href="<?php echo $services['cta_link_3']['url']; ?>" title="<?php echo $services['cta_link_3']['title']; ?>" class="service">
                                            <h3><?php echo $services['ydelse_3_overskrift']; ?> <i class="fa-solid fa-hashtag"></i></h3>
                                            <?php echo $services['ydelse_3_tekst']; ?>
                                            <span class="readmore"><?php echo $services['cta_link_3']['title']; ?> <i class="fa-solid fa-arrow-right-long"></i></span>
                                        </a>
    
                                        <a href="<?php echo $services['cta_link_4']['url']; ?>" title="<?php echo $services['cta_link_4']['title']; ?>" class="service">
                                            <h3><?php echo $services['ydelse_4_overskrift']; ?> <i class="fa-solid fa-arrow-trend-up"></i></h3>
                                            <?php echo $services['ydelse_4_tekst']; ?>
                                            <span class="readmore"><?php echo $services['cta_link_4']['title']; ?> <i class="fa-solid fa-arrow-right-long"></i></span>
                                        </a>
                                    </div>
                                    <div class="focus_cta">
                                        <a href="#" title="Få tilsendt et overblik over alle ydelserne" class="cta">Få tilsendt et overblik over alle ydelserne <i class="fa-solid fa-arrow-right-long"></i></a>
                                    </div>
                                </div>
                            <?php endif; ?>
    
                            <?php
                            $cases = get_field('forside_cases');
                            
                            if( $cases ): ?>
    
                                <div class="cases-section">
                                    <h2><?php echo $cases['cases_overskrift']; ?></h2> 
                                    <div class="container">
                                        <div class="case-text col col--xs-12 col--sm-12 col--md-6 col--lg-6">
                                            <p class="title"><?php echo $cases['case_virksomhedsnavn']; ?></p>
                                            <p class="text"><?php echo $cases['case_tekst']; ?></p>
                                            
                                            <?php 
                                            $active_services = get_field("aktive_ydelser");
    
                                            //var_dump($active_services);
    
                                            if( $active_services ): 
                                            ?>
                                            <p>Samarbejdet indeholder bla.</p>
                                            <ul>
                                                <?php foreach ( $active_services as $active_service): ?>
                                                <li class="active-service"><?php echo $active_service; ?></li>
                                                <?php endforeach; ?>
                                            </ul>
                                            <?php endif; ?>
    
                                            <a href="#" title="Se cases" class="cta">Send mig et forslag på et samarbejde <i class="fa-solid fa-arrow-right-long"></i></a>
    
                                        </div>
                                        <div class="case-image col col--xs-12 col--sm-12 col--md-6 col--lg-6">
                                            <img src="<?php echo $cases['case_billede_url']; ?>" alt="<?php echo $cases['case_virksomhedsnavn']; ?>">
                                        </div>
                                    </div>
                                </div>
    
                            <?php endif; ?>
                            
                        </div>
    
            <?php endwhile; ?>
    
        </div>
    </main>
    
    <?php get_footer(); ?>
    
  • Is your checkbox field a sub field of another field? a sub field of a group field maybe?

  • The checkbox field is inside a group.
    Field Group => Field type: Group => Field Type: Checkbox

    Is it that?

  • then is is a subfield and you need to use a have_rows() loop on the group field an get_sub_field() for the checkbox field.

    Or access it from the array. $cases = get_field('forside_cases');

    
    $cases['aktive_ydelser']
    
  • Thank you so much!

    I made it work with just getting them from $cases array.

    
     <?php
                            $cases = get_field('forside_cases');
                            
                            if( $cases ): ?>
    
                                <div class="cases-section">
                                    <h2><?php echo $cases['cases_overskrift']; ?></h2> 
                                    <div class="container">
                                        <div class="case-text col col--xs-12 col--sm-12 col--md-6 col--lg-6">
                                            <p class="title"><?php echo $cases['case_virksomhedsnavn']; ?></p>
                                            <p class="text"><?php echo $cases['case_tekst']; ?></p>
                                            
                                            <?php 
    
                                            $active_services = $cases['aktive_ydelser'];
    
                                            if( $active_services ): 
                                            ?>
                                            <p>Samarbejdet indeholder bla.</p>
                                            <ul>
                                                <?php foreach ( $active_services as $active_service): ?>
                                                <li class="active-service"><?php echo $active_service; ?></li>
                                                <?php endforeach; ?>
                                            </ul>
                                            <?php endif; ?>
    
                                            <a href="#" title="Se cases" class="cta">Send mig et forslag på et samarbejde <i class="fa-solid fa-arrow-right-long"></i></a>
    
                                        </div>
                                        <div class="case-image col col--xs-12 col--sm-12 col--md-6 col--lg-6">
                                            <img src="<?php echo $cases['case_billede_url']; ?>" alt="<?php echo $cases['case_virksomhedsnavn']; ?>">
                                        </div>
                                    </div>
                                </div>
    
                            <?php endif; ?>
    
Viewing 13 posts - 1 through 13 (of 13 total)

You must be logged in to reply to this topic.