如何在WordPress中关闭/body标签后添加Javascript


How to add Javascript after closing /body tag in WordPress

我试图在几个WordPress页面内嵌入iFrame。每个页面将使用不同的iFrame内容。这是一个商务网站,所以需要第一个iframe是客户的登录。不同WP页面上的后续iframe将用于产品搜索,结帐…(我的问题在下面))

  1. 在header.php中,我放置了一些需要包含在每个WordPress页面上的初始代码。

  2. 我在每个页面的正文中添加了iframe

  3. 我需要将iframe初始化为特定的iframe内容。我想将此代码放置在结束的</body>标记之后,如下所示:


 <html>
    <head>
     <script>
      var absDomain =  "http://www.webstore.com/"
      var iframeObj;
      var timestampString = '' + new Date().getTime() + ''
       document.write('<script type="text/javascript" src="' + absDomain + 'Support.js?'+timestampString+'"><'/script>');
       document.write('<script type="text/javascript" src="' + absDomain + 'ProductSearch.js?'+timestampString+'"><'/script>');
       document.write('<script type="text/javascript" src="' + absDomain + 'Environment.js?'+timestampString+'"><'/script>');
      function gotoIframe(theLocation){
        var timeStamp = "";
        iframeObj = document.getElementById("iframeContent");
        timeStamp = "&" + new Date().getTime();
        iframeObj.src = absDomain + theLocation + "?iframe=yes" + timeStamp;
      }
      function executeIFrameProductSearch(str1, str2){
        goTo_CategoryForIframe(str1, str2, document.getElementById("iframeContent"));
      }
     </script>
    </head>
    <body>
      <!-- iFrame embedded below -->
      <iframe id="iframeContent" style="width:100%;height:500px; " src=""></iframe>
    </body>
     <script>
      //script needed for initial iframe page and iframe support.
      iframeObj = document.getElementById("iframeContent");
      gotoIframe("CustomerLogin.html");
     </script>
    </html>
    //After the above code section (after the </body> tag) is embedded into the Login page, I expect to use:
    //Customer Login:
    <a href="javascript:gotoIframe('CustomerLogin.html')">Customer Login</a>
    //Product Search:
    <a href="javascript:gotoIframe('ProductSearch.html')">Product Search</a>

问题:我在哪里插入每个特定WordPress页面的结束标签后的代码?谢谢。

将script标签放在body标签之后是无效的,但是如果您真的想这样做,你可以查看主题的footer.php.

例如,您可以在footer.php中找到这样的代码:

<?php
    </div><!-- close tag for content container -->
?>
    <footer>
    <!-- footer content -->
    </footer>
    </body>
    <!-- here is the place to put your code -->
</html>

复制此函数并粘贴到functions.php文件

       //alert(); enable alert to test
          //script needed for initial iframe page and iframe support.`function your_function() {
echo '<script>`
          iframeObj = document.getElementById("iframeContent");
          gotoIframe("CustomerLogin.html");
     </script>';

}

add_action('wp_footer', 'your_function', 100);//这将在每个页面的body标签后添加脚本

其他选项将是//剩下的函数。php在这一行以上或任何地方!

函数yourprefix_scripts_after_opening_body_tag() {$javascript = file_get_contents(get_template_directory())。'/js/opening-body.js ');回声"。美元的javascript。";}

add_action('yourprefix_after_opening_body_tag', 'yourprefix_scripts_after_opening_body_tag');

将此代码粘贴到想要显示

的footer.php中