Search code examples
phpwordpressif-statementwordpress-theming

Trying to modify this php code "if" statement in WordPress


This is custom code in WordPress theme files I'm trying to modify this PHP code "if" statement. The first part checks to see if the page is in French, else it will show English. However, the second part "Apply Now" link is ONLY in English. SO I need an "if" statement like the first part.

This is the original code:

echo '<main>';

        if (pll_current_language() == 'fr') {
          echo '<h4 class="category-title">Adhésion pour membres de l’industrie (catégorie B)</h4>';
        }
        else {
          echo '<h4 class="category-title">Industry Membership (Category B)</h4>';
        }
        echo '<p>' . $category_b_information . '</p>';
        if ($application_link_b) {
          echo '<a href="' . $application_link_b . '" class="button" target="_blank" rel="noopener">Apply Here</a>';
        }

      echo '</main>';


This is what I tried but I got an error:

echo '<main>';

if (pll_current_language() == 'fr') {
  echo '<h4 class="category-title">Adhésion pour membres de l’industrie (catégorie B)</h4>';
}
else {
  echo '<h4 class="category-title">Industry Membership (Category B)</h4>';
}

if (pll_current_language() == 'fr') {
    echo '<p>' . $category_b_information . '</p>';
    if ($application_link_b) {
        echo '<a href="' . $application_link_b . '" class="button" target="_blank" rel="noopener">appliquer ici</a>';
    }
    else {
        echo '<p>' . $category_b_information . '</p>';
    if ($application_link_b) {
       echo '<a href="' . $application_link_b . '" class="button" target="_blank" rel="noopener">Apply Here</a>';
    }

echo '</main>';

Solution

  • There are multiple mistakes in your code, try the following instead:

    echo '<main>';
    
    if (pll_current_language() == 'fr') {
        echo '<h4 class="category-title">Adhésion pour membres de l’industrie (catégorie B)</h4>';
        echo '<p>' . $category_b_information . '</p>';
        if ($application_link_b) {
            echo '<a href="' . $application_link_b . '" class="button" target="_blank" rel="noopener">appliquer ici</a>';
        }
    }
    else {
        echo '<h4 class="category-title">Industry Membership (Category B)</h4>';
        echo '<p>' . $category_b_information . '</p>';
        if ($application_link_b) {
            echo '<a href="' . $application_link_b . '" class="button" target="_blank" rel="noopener">Apply Here</a>';
        }
    }
    echo '</main>';
    

    It should work.