0

I have a custom field in a WP post called "profession." If this profession is RN, then I want one piece of text displayed. If the profession is not RN, then I want another piece of text displayed.

I added the following code to execute this function:

        <?php if ( $profession = 'RN' ) {
        echo '<li>Minimum 2 years experience</li><li>Current license in this state</li><li>Graduated from accredited Nursing school</li><li>BCLS required</li><li>BSN and ACLS preferred</li><li>Other requirements to be determined by our client facility</li>';
    } else {
        echo '<li>Minimum 2 years experience</li><li>Other requirements to be determined by our client facility</li>';
    } ?>

The problem is, the first echo statement is displaying even when the post does not have RN in the profession field. If I purposely break the if variable, then it defaults to the second echo statement across the board. I can't get the post to respond dynamically to one vs the other.

Am I missing something in this code that is causing one or the other to display for all instead of based on the parameter I am trying to set?

3 Answers3

0

You need to compare in your if not assign.

=  is used to assign values
== is used for comparison, checks if value is equal
=== is used for strict comparison, checks if value and type are equal.

So your php if statement should look like this

if ( $profession == 'RN' ){
    code
}else{
    code
}    
Echtniet
  • 200
  • 2
  • 11
0

Please read how to use condition before writing.

http://php.net/manual/en/control-structures.if.php

 <?php if ( $profession == 'RN' ) {
        echo '<li>Minimum 2 years experience</li><li>Current license in this state</li><li>Graduated from accredited Nursing school</li><li>BCLS required</li><li>BSN and ACLS preferred</li><li>Other requirements to be determined by our client facility</li>';
    } else {
        echo '<li>Minimum 2 years experience</li><li>Other requirements to be determined by our client facility</li>';
    } ?>
jvk
  • 2,133
  • 3
  • 19
  • 28
  • That's the site I used originally to create the code. Using the modified equals effectively kills the first line. It works when I use "=" but fails when I use "==" – Kyle Boreing Apr 20 '18 at 19:00
  • @KyleBoreing, if you give = means you are assign a variable – jvk Apr 20 '18 at 19:03
  • Here is https://www.w3schools.com/php/php_variables.asp how to create a variable – jvk Apr 20 '18 at 19:04
  • The problem is not the variable. I already have this as a custom field, and it is recognized when I use "=" but not when I use "==," as stated previously. My problem is not getting the first line to display. My problem is hiding it when any other variable is in place. I have posts that do not include that particular variable but are still displaying the first echo statement anyway. – Kyle Boreing Apr 23 '18 at 12:20
  • I've even tried mapping the custom field to integers (RN=1, CT Tech=2, etc.), and I have the same problem. The first if statement is all or nothing. It will not recognize any difference unless I break it, then it defaults to the second statement. – Kyle Boreing Apr 23 '18 at 17:28
  • Hi @KyleBoreing, please paste your full code – jvk Apr 23 '18 at 17:29
  • I've also tried not using echo statements and closed if/then statements, and the same thing happens. – Kyle Boreing Apr 23 '18 at 17:40
-1
<?php if ( $profession == 'RN' ) {
    echo '<li>Minimum 2 years experience</li><li>Current license in this      state</li><li>Graduated from accredited Nursing school</li><li>BCLS required</li>   <li>BSN and ACLS preferred</li><li>Other requirements to be determined by our client facility</li>';
} else {
    echo '<li>Minimum 2 years experience</li><li>Other requirements to be determined by our client facility</li>';
} ?>