-1

this is what my code looks like: Any help will be very appreciable.

    <?php
        $btn = get_field('line_btn');
        if( $btn && in_array('study', $btn ) ) { 
          echo 'study';       // working well
        }
        elseif( $btn && in_array('help', $btn ) ) { 
          echo 'help';        // working well
        }
        elseif( $btn && in_array('study', 'help', $btn) ) { 
          echo 'both selected';     // not working
        }
      ?>
  • Your question lacks necessary details. Please read [mre] – Howard E Apr 21 '22 at 12:11
  • You should tell us what is actually causing you problems. A [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) would also help a great deal. – geertjanknapen Apr 21 '22 at 14:10
  • Please provide enough code so others can better understand or reproduce the problem. – Community Apr 21 '22 at 14:10
  • In Advanced Custom FIelds I created a **checkbox** with two options. lets say **'study'** and **'help'**. So if the admin selects _'study'_, then it should return certain code like _
    ....
    _. And if the admin selects _'help'_, then it should return certain code like _
    .....
    _. And lastly if admin selects **both** then it should return both like _
    .....
    ....
    _. My code works well for returning **separate value**. But it is not working well if the admin selects **both options**.
    – Shin-chyan Apr 22 '22 at 01:11

1 Answers1

0

in_array() only checks for one value in the array and you are trying to pass in two. You would have to us two separate in_array() functions in the elseif.

<?php
        $btn = get_field('line_btn');
        if( $btn && in_array('study', $btn ) ) { 
          echo 'study';       // working well
        }
        elseif( $btn && in_array('help', $btn ) ) { 
          echo 'help';        // working well
        }
        elseif( $btn && in_array('study', $btn) && in_array( 'help', $btn ) { 
          echo 'both selected';
        }
      ?>
JayDev95
  • 777
  • 2
  • 5
  • 18
  • **Thanks a lot** @JayDev95 Is there a way that I can shorten the code like: `` – Shin-chyan Apr 25 '22 at 06:21
  • @Shin-chyan That logic wouldn't work for what you are looking for. If you are looking to make the template file neater, you have different options. You can either place this in a function and call that function, or put a custom action hook where you need this to show and then add that function to that action hook. – JayDev95 Apr 25 '22 at 10:28
  • Thank you. But I am looking for another way other than writing that in a function – Shin-chyan Apr 27 '22 at 01:04
  • @Shin-chyan You can create a shortcode if that'll work better for you. – JayDev95 Apr 27 '22 at 01:13