Support

Account

Home Forums Front-end Issues Using Checkbox conditional logic in a loop

Solved

Using Checkbox conditional logic in a loop

  • How to do a conditional logic for a checkbox value in a loop?.
    My custom checkbox field is only assigned into a page, it only has 1 checkbox. What I am trying to do is to display all the page on my homepage where the checkbox value is true.

    here is my code
    <div class=”one-third column”>

    <?php
    $args = array(
                        'numberposts' => 5,
                        'post_type' => 'page',
                        'meta_query' =>
                            array(
                                'key' => 'featured_page',
                                'value' => 'yes',
                            )
                    );
    
                    // get results
                    $the_query = new WP_Query( $args );
    
                    // The Loop
                    ?>
                    <?php if( $the_query->have_posts() ): ?>
                        <ul>
                            <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
                                <li>
                                    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                </li>
                            <?php endwhile; ?>
                        </ul>
                    <?php endif; ?>
    
                    <?php wp_reset_query();?>
    
    </div>

    Thanks,
    Mark

  • Hi @markyeoj

    Your code looks good. Does it work? If not, what part doesn’t work?

    Is your ‘checkbox’ field actually a checkbox field with a ‘yes’ choice? Or is it actually a true / false field?

    Thanks
    E

  • Thanks @elliot for response, its not working because I only assigned 5 for number of posts, but it displays 10 pages, one more problem is I only checked one page but like what I said it displays 10 pages in my homepage.

    Here is the image of my setup.

  • Fixed.
    I used true/false instead of checkbox field and used this code.

    $args = array(
                'post_type' => 'page',
                'meta_key' =>'featured_page',
                'meta_value'=>'1'
            );
    
                    // get results
                    $the_query = new WP_Query( $args );
    
                    // The Loop
                    ?>
                    <?php if( $the_query->have_posts() ): ?>
                        <ul>
                            <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
                                <li>
                                    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                </li>
                            <?php endwhile; ?>
                        </ul>
                    <?php endif; ?>
    
                    <?php wp_reset_query(); ?>
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Using Checkbox conditional logic in a loop’ is closed to new replies.