Home › Forums › General Issues › Get Field Values outside the Loop
Hello, I have a problem with the following Situation. My Code works fine, but there is one Problem. The whole definition of Variables is inside the loop, because they don’t work outside (ACF Fields). But I need to use them in de IF Statement of the loop. Has anyone an idea? I want to have the following Part inside the If Statement of the loop:
$years < 1 && $months < 1
and there also should be checked, if the fields have content or are empty.
Here is my Complete Code:
<?php
$args = array(
'post_type' => 'fahrzeuge',
);
$custom_query = new WP_Query($args);
if ($custom_query->have_posts()) : while ($custom_query->have_posts()) : $custom_query->the_post(); ?>
<!-- Start der geloopten Inhalte -->
<p>
<?php
$fahrzeug = get_the_title();
$heute = date('d.m.Y');
$service_datum = get_field('fahrzeug_naechster_service');
$dateDifference = abs(strtotime($service_datum) - strtotime($heute));
$years = floor($dateDifference / (365 * 60 * 60 * 24));
$months = floor(($dateDifference - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
$days = floor(($dateDifference - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));
if ($years < 1 && $months < 1 && strtotime($service_datum) > strtotime($heute)) {
echo "Service für <b>" . $fahrzeug . "</b> fällig in <b>" . $days . "</b> Tagen. " . strtotime($service_datum) . " " . strtotime($heute);
} elseif ($years < 1 && $months < 1 && strtotime($service_datum) == strtotime($heute)) {
echo "Service für <b>" . $fahrzeug . "</b> fällig <b>Heute</b> " . strtotime($service_datum) . " " . strtotime($heute);
} elseif ($years < 1 && $months < 1 && strtotime($service_datum) < strtotime($heute)) {
echo "Service für <b>" . $fahrzeug . "</b> war fällig vor <b>" . $days . "</b> Tagen. " . strtotime($service_datum) . " " . strtotime($heute);
}
?>
</p>
<!-- Ende der geloopten Inhalte -->
<?php endwhile;
else : ?>
<p>Kein Service oder Wartung in den nächsten 30 Tagen!</p>
<?php endif;
wp_reset_postdata(); ?>
The Problem with the actual Code is, that the IF Statement of the Loop is always true and the Else Statement will not show even if the IF Statements inside the Loop are false. And if I put an Else into the Loop, my message appears again and again for every Post queried by the loop.
Has anyone an idea?
Greetings, Alex
You need to loop over your fields to see if there is content and use a flag varialbe. You also need to move the <p>
tags inside your conditions to prevent outputting empty paragraph tags.
$has_content = false; // flag
if ($custom_query->have_posts()) {
while ($custom_query->have_posts()) {
$custom_query->the_post();
// get your field values
// check if something should be displayed
if (/*your comparison*/) {
// set flag because something is shown
$has_content = true;
echo '<p>the rest of your content</p>';
}
} // end while
} // end if
// check the flag
if (!$has_content) {
echo '<p>your no content message</p>'
}
You must be logged in to reply to this topic.
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.