search

Home  >  Q&A  >  body text

Using if else conditional statements in WordPress functions

I have the following code:

function wpb_hook_javascript() {
  if (is_page( array( 244, 174, 697, 702, 699, 708, 713 ) ) ) { 
    ?>
      <noscript>
            <iframe src="FORM_ONE" width="100%" height="550" type="text/html" frameborder="0" allowTransparency="true" style="border: 0"></iframe>
      </noscript>

      <script type="text/javascript">
            var form = 'FORM_ONE';
            var params = window.location.search;
            var thisScript = document.scripts[document.scripts.length - 1];
            var iframe = document.createElement('iframe');

            iframe.setAttribute('src', form + params);
            iframe.setAttribute('width', '100%');
            iframe.setAttribute('height', 550);
            iframe.setAttribute('type', 'text/html');
            iframe.setAttribute('frameborder', 0);
            iframe.setAttribute('allowTransparency', 'true');
            iframe.style.border = '0';

            thisScript.parentElement.replaceChild(iframe, thisScript);
      </script>
      
    <?php }

  elseif (is_page( array( 187, 283, 285, 287, 18608, 18619, 18629, 295, 297, 299, 303, 305, 307 ) ) ) { ?>
      <noscript>
            <iframe src="FORM_TWO" width="100%" height="550" type="text/html" frameborder="0" allowTransparency="true" style="border: 0"></iframe>
      </noscript>

      <script type="text/javascript">
            var form = 'FORM_TWO';
            var params = window.location.search;
            var thisScript = document.scripts[document.scripts.length - 1];
            var iframe = document.createElement('iframe');

            iframe.setAttribute('src', form + params);
            iframe.setAttribute('width', '100%');
            iframe.setAttribute('height', 550);
            iframe.setAttribute('type', 'text/html');
            iframe.setAttribute('frameborder', 0);
            iframe.setAttribute('allowTransparency', 'true');
            iframe.style.border = '0';

            thisScript.parentElement.replaceChild(iframe, thisScript);
      </script>
        
    <?php }

    else (is_page( array( 205, 399, 401, 403, 405, 407 ) ) ) {  ?>
      <noscript>
            <iframe src="FORM_THREE" width="100%" height="550" type="text/html" frameborder="0" allowTransparency="true" style="border: 0"></iframe>
      </noscript>

      <script type="text/javascript">
            var form = 'FORM_THREE';
            var params = window.location.search;
            var thisScript = document.scripts[document.scripts.length - 1];
            var iframe = document.createElement('iframe');

            iframe.setAttribute('src', form + params);
            iframe.setAttribute('width', '100%');
            iframe.setAttribute('height', 550);
            iframe.setAttribute('type', 'text/html');
            iframe.setAttribute('frameborder', 0);
            iframe.setAttribute('allowTransparency', 'true');
            iframe.style.border = '0';

            thisScript.parentElement.replaceChild(iframe, thisScript);
      </script>
    <?php }  

}
add_action('wp_head', 'wpb_hook_javascript');

Based on the list of pages in the array, I need to provide different forms. I currently have forms such as FORM_ONE. This code currently breaks my site.

If I remove the elseif & else it works, but I still have a lot to add.

Can anyone tell me what I'm doing wrong?

TIA

P粉176980522P粉176980522237 days ago306

reply all(2)I'll reply

  • P粉663883862

    P粉6638838622024-03-31 09:16:49

    Delete the endif; on the bottom line.

    If you want to use endif;, you must use the following syntax:

    
    
        { html goes here }
    
    
    
        { html goes here }
    
    
    
        { html goes here }
    
    

    reply
    0
  • P粉373596828

    P粉3735968282024-03-31 00:16:16

    The problem is in this case:

    else (is_page( array( 205, 399, 401, 403, 405, 407 ) ) ) {  ?>

    Change else to elseif or remove the condition after else

    reply
    0
  • Cancelreply