I have a Wordpress loop where I want to load the right template based on the value in a variable called 'is_neighborhood'. In the code below, my echo statement prints "Yes" on the screen. However, it gets to the if and else statements and writes "Booooooo" when it should be writing "Whoo Hoo" based on the value of 'is_neighborhood'
Why is this not evaluating properly?
while ( have_posts() ) :
the_post();
echo the_field('is_neighborhood');
if (the_field('is_neighborhood') == 'Yes')
{
echo 'WHOO HOO!';
die();
}
else
{
echo 'Booooooo';
die();
}
get_template_part( 'template-parts/content/content', 'single' );
// End the loop.
endwhile;
I have a Wordpress loop where I want to load the right template based on the value in a variable called 'is_neighborhood'. In the code below, my echo statement prints "Yes" on the screen. However, it gets to the if and else statements and writes "Booooooo" when it should be writing "Whoo Hoo" based on the value of 'is_neighborhood'
Why is this not evaluating properly?
while ( have_posts() ) :
the_post();
echo the_field('is_neighborhood');
if (the_field('is_neighborhood') == 'Yes')
{
echo 'WHOO HOO!';
die();
}
else
{
echo 'Booooooo';
die();
}
get_template_part( 'template-parts/content/content', 'single' );
// End the loop.
endwhile;
Share
Improve this question
asked Mar 5 at 22:26
Chris FarrugiaChris Farrugia
1,0584 gold badges17 silver badges36 bronze badges
5
|
1 Answer
Reset to default 2the_field('is_neighborhood') directly prints the value. it returns null. Use get_field('is_neighborhood') on place of it.
$is_neighborhood = get_field('is_neighborhood'); // Get the field value
echo $is_neighborhood; // Print it to verify
if ($is_neighborhood == 'Yes') {
var_dump(the_field('is_neighborhood'))
? Then you can see exactly what is being returned by that call. – Rob Eyre Commented Mar 5 at 22:37the_field()
, so please post the code so that we can see what it is doing. – Tangentially Perpendicular Commented Mar 5 at 23:28the_field()
does echo itself the value, so removeecho
… Now in yourIF
statement, you need to replace the functionthe_field()
withget_field()
... – LoicTheAztec Commented Mar 6 at 0:39