Home › Forums › Front-end Issues › True/False Not Working after adding post
hi i have a big problem i create a true/false custom filed for default post type. and this custom field act like featured post.. eg: if true, news will show in front page slider. if not, it is a normal post. so my question and problem is when i set featured post one by one it shows featured slider smoothly but when i publish a couple of normal post (3 or more) the featured post are gone.. my font end code is :
<?php $temp = $wp_query; $wp_query= null; $wp_query = new WP_Query(); $wp_query->query('showposts=5' . '&paged='.$paged ); while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<?php if( get_field('featured_news')): ?>
<li>
<div class="left">
<h1><?php the_title(); ?></h1>
<h2><?php if(the_field('sub_title') != ''){ ?> <?php the_field('sub_title'); ?> <?php } ?></h2>
<?php get_the_content_limit(250, 'Read more →'); ?>
</div>
<div class="right">
<div>
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'main_slider_image'); ?></a>
</div>
</div>
</li>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
<?php endwhile; ?>
the front end template snap is below.. (figure 1)
and my backend post list is .. (figure 2)
and my field setting are (figure 3)
Please Please Please help me….
Hello, sorry to not answer to your question directly, but I proposed a different approach.
why not use an another category “slider” per example. With it, you just need to filter in the loop this category ? In the table of posts, the authors will see which post are tagged “slider” or not (In your Figure2 it is not possible actually). I thinking it will be more ergonomic for them.
Hi @Yashi
There is a logic error in your code. You are loading 5 posts, but then within the loop you are checking for the value of ‘featured_news’.
If none of the 5 latest posts have this selected, then no HTML will be rendered.
What you indented to do was to query the posts with the a ‘featured_news’ value of 1. You can do this by following the tutorial here:
http://www.advancedcustomfields.com/resources/how-to/how-to-query-posts-filtered-by-custom-field-values/
To sum up, you want to query posts that have the custom field value, then remove the if statement completely
Thanks
E
@ Elliot your solution work fine… i apply first example method all good and i apply showposts => 5 and ordering method to show only latest 5 posts..it works. now i don’t need to create a category for featured news. custom field do the rest. thank you Elliot.
The topic ‘True/False Not Working after adding post’ 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.