最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - String evaluation not working. Why isn't this if statement evaluating to true? - Stack Overflow

programmeradmin3浏览0评论

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
  • Insteading of echoing, have you tried doing 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:37
  • Thanks @RobEyre for that. It's weird because echoing produces "Yes" but var_dump produces NULL. – Chris Farrugia Commented Mar 5 at 23:28
  • The key here is the return value of the_field(), so please post the code so that we can see what it is doing. – Tangentially Perpendicular Commented Mar 5 at 23:28
  • @TangentiallyPerpendicular That code is from some plugin I'm using. I wouldn't know how to do that. Sorry! – Chris Farrugia Commented Mar 5 at 23:31
  • ACF function the_field() does echo itself the value, so remove echo… Now in your IF statement, you need to replace the function the_field() with get_field()... – LoicTheAztec Commented Mar 6 at 0:39
Add a comment  | 

1 Answer 1

Reset to default 2

the_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') {
发布评论

评论列表(0)

  1. 暂无评论