Home › Forums › General Issues › Using the true/false field to decide if a post is displayed
Hello, I am using the ACF plugin and i’ve ran into an issue using the true/false field.
I basically want to have a true/false option on every post saying whether it should be displayed on a certain page or not.
Currently This page displays every ‘channel’ post where as I want it to display every ‘channel’ post other than those with the option saying it shouldn’t be display.
I don’t really have an idea of how I would do this and would greatly appreciate some help.
This is the code that pulls the post into the page –
<section id="channels" class="container">
<div class="rowOpen clearfix">
<div class="twelvecol">
<div id="masonary" class="nomargin">
<?php
$channels = new WP_Query(array('post_type'=> 'channels'));
while ($channels->have_posts()) : $channels->the_post();
?>
<div class="mason galleryItem">
<div class="img-contain border">
<a href="<?php the_field('channel_link'); ?>" target="_blank" class="img-shiv">
<?php $image = get_field('channel_logo');
if(!empty($image)) {
$imageResized = vt_resize( '', $image, 183, 183, false );
$imageTitle = get_the_title();
echo '<img src="'.$imageResized[url].'" height="'.$imageResized[height].'" width="'.$imageResized[width].'" alt="'.$imageTitle.'" class="statImg" />';
}
?>
</a>
</div>
</div>
<?php endwhile; ?>
</div>
</div>
</div>
</section>
Thanks in advance, Harry.
Hi @harryadf
Within the loop, but before any ‘cahnnel’ code is output, you can conditionaly check the true_false value and skip the loop if necessary like so:
<?php while ($channels->have_posts()) : $channels->the_post(); ?>
<?php if( ! get_field('true_false_field_name')){ continue; } ?>
<?php // channel code here ?>
<?php endwhile; ?>
I have added it like so –
<section id="channels" class="container">
<div class="rowOpen clearfix">
<div class="twelvecol">
<div id="masonary" class="nomargin">
<?php
$channels = new WP_Query(array('post_type'=> 'channels'));
while ($channels->have_posts()) : $channels->the_post();
if( ! get_field('show_on_channel_page')){ continue; }
?>
<div class="mason galleryItem">
<div class="img-contain border">
<a href="<?php the_field('channel_link'); ?>" target="_blank" class="img-shiv">
<?php $image = get_field('channel_logo');
if(!empty($image)) {
$imageResized = vt_resize( '', $image, 183, 183, false );
$imageTitle = get_the_title();
echo '<img src="'.$imageResized[url].'" height="'.$imageResized[height].'" width="'.$imageResized[width].'" alt="'.$imageTitle.'" class="statImg" />';
}
?>
</a>
</div>
</div>
<?php endwhile; ?>
</div>
</div>
</div>
</section>
and I still can’t seem to get it to work.
Sorry for my incompetence but i’m fairly new to this.
Thanks, Harry.
<section id="channels" class="container">
<div class="rowOpen clearfix">
<div class="twelvecol">
<div id="masonary" class="nomargin">
<?php
$channels = new WP_Query(array('post_type'=> 'channels'));
while ($channels->have_posts()) : $channels->the_post();
if( ! get_field('show_on_channel_page')){ continue; } ?>
<div class="mason galleryItem">
<div class="img-contain border">
<a href="<?php the_field('channel_link'); ?>" target="_blank" class="img-shiv">
<?php $image = get_field('channel_logo');
if(!empty($image)) {
$imageResized = vt_resize( '', $image, 183, 183, false );
$imageTitle = get_the_title();
echo '<img src="'.$imageResized[url].'" height="'.$imageResized[height].'" width="'.$imageResized[width].'" alt="'.$imageTitle.'" class="statImg" />';
}
?>
</a>
</div>
</div>
<?php endwhile; ?>
</div>
</div>
</div>
</section>
(Thought i’d add it without any indents to make it easier to read)
My bad, I had to go through each post and update them one by one, huge thanks for your help and happy holidays!
Thanks, Harry.
The topic ‘Using the true/false field to decide if a post is displayed’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.