首頁  >  問答  >  主體

在WordPress函數中使用if else條件語句

我有以下程式碼:

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');

根據數組中的頁面列表,我需要提供不同的表單。我目前有 FORM_ONE 等表單。此程式碼目前破壞了我的網站。

如果我刪除 elseif & else 它會起作用,但我還有很多要添加的內容。

誰能告訴我我做錯了什麼?

TIA

P粉176980522P粉176980522206 天前275

全部回覆(2)我來回復

  • P粉663883862

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

    刪除底行的endif;

    如果您想使用endif;,您必須使用以下語法:

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

    回覆
    0
  • P粉373596828

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

    問題是在這種情況下:

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

    將 else 更改為 elseif 或刪除 else 之後的條件

    回覆
    0
  • 取消回覆