简体   繁体   English

wp_add_inline_script没有向我的页面添加任何内容,无法弄清原因

[英]wp_add_inline_script isn't adding anything to my page, can't figure out why

I want to add inline script to just a single page, so trying to add in the page template file. 我想将内联脚本仅添加到单个页面,因此尝试添加页面模板文件。 However, both placements of the code above or below the footer call results in nothing being outputted in the source. 但是,在页脚调用上方或下方的两个代码放置都不会在源中输出任何内容。

I can't figure out how this is supposed to work, can anyone help? 我不知道这应该如何工作,有人可以帮忙吗?

** Page Template ** **页面模板**

<?php
get_footer('blank');
function cardpay_demo_script() {
    wp_add_inline_script(
        'jquery',
        '$(".btn-next").click(function() {
            $(".nav-pills .active").parent().next("li").find("a").removeClass("disabled").trigger("click");
        });'
    );
}
add_action('wp_enqueue_scripts', 'cardpay_demo_script');

** Footer File ** **页脚文件**

<?php wp_footer(); ?>
</body>
</html>

You are placing the function on the wrong file. 您将函数放置在错误的文件上。 The function should go on the theme's functions.php file and not any default template file or your custom template file . 该功能应位于主题的functions.php文件中,而不应位于任何默认模板文件或自定义模板文件中

The reason for this is that in WordPress; 原因是在WordPress中; an Action is a PHP function that is executed at specific points throughout the WordPress Core and since your custom templates will be called way after the hook has been triggered the function will never actually run, so it won't print anything. 一个Action是一个PHP函数,它在整个WordPress Core的特定位置执行,由于自定义模板将在触发挂钩之后被调用,因此该函数将永远不会真正运行,因此不会输出任何内容。

You can use is_page() or is_page_template() to determine if it is the page or page template you're looking for. 您可以使用is_page()is_page_template()来确定它是您要查找的页面还是页面模板。

Here is some additional information on action hooks: https://codex.wordpress.org/Glossary#Action 以下是有关动作挂钩的其他信息: https : //codex.wordpress.org/Glossary#Action

add_action( 'wp_enqueue_scripts', function () {
   if( !is_page_template('My Template') ) return;
   if ( ! wp_script_is( 'jquery', 'done' ) ) {
     wp_enqueue_script( 'jquery' );
   }
   wp_add_inline_script( 'jquery', '$(".btn-next").click(function() { $(".nav-pills .active").parent().next("li").find("a").removeClass("disabled").trigger("click");
        });' );
});

or you can just print your scripts on the footer using the wp_footer hook. 或者您也可以使用wp_footer钩子在脚注上打印脚本。 Just put this on your theme's functions.php file 只需将其放在主题的functions.php文件中

add_action('wp_footer', function (){ 
   if( !is_page_template('My Template') ) return;
?>
    <script type="text/javascript">
        jQuery(".btn-next").click(function() { jQuery(".nav-pills .active").parent().next("li").find("a").removeClass("disabled").trigger("click");
            });
    </script>

<?php   
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的innerHTML命令没有更改任何内容,我不知道为什么 - My innerHTML command isn't changing anything and i can't figure out why 无法弄清楚为什么我的JavaScript没有做任何事情 - Can't figure out why my javascript isn't doing anything 似乎无法弄清楚该脚本为何不起作用 - Can't seem to figure out why the script isn't working 如何将延迟或异步属性添加到wp_add_inline_script? - How to add defer or async attribute to wp_add_inline_script? 我无法弄清楚为什么我添加事件处理程序的简单尝试无效 - I can't figure out why my simple attempt at adding an event handler isn't working 试图在 Javascript 中创建一个“trie”。 无法弄清楚为什么我的方法没有正确添加孩子 - Trying to create a 'trie' in Javascript. Can't figure out why my method isn't adding the children correctly 无法弄清楚为什么我的Javascript没有使用Bulma框架更改CSS - Can't figure out why my css isn't being changed by my Javascript using Bulma framework jQuery Isotope插件-添加内联“ display:none”-无法弄清原因 - jQuery Isotope Plugin - Adding inline “display:none” - can't figure out why 无法弄清楚为什么JS插件无法正常工作 - Can't figure out why JS plugin isn't working 无法弄清楚为什么它不起作用了 - Can't figure out why it isn't working, anymore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM