简体   繁体   English

在 Wordpress 中使用 wp_enqueue

[英]Using wp_enqueue in Wordpress

I turn to this awesome community , after days of trying to fix this bug, my problem is really simple, but it has got to me.我求助于这个很棒的社区,经过几天的尝试修复这个错误,我的问题真的很简单,但它已经到了我身上。 I am trying to enqueue a Java script to my Theme my login Plugin while using the evolve theme.我正在尝试在使用进化主题时将 Java 脚本排队到我的主题我的登录插件中。

here is the code snippet that does that, and i used the global function to check if the function is being loaded , and it is not being loaded.这是执行此操作的代码片段,我使用全局函数来检查该函数是否正在加载,并且没有被加载。 'a' does not change to true. 'a' 不会变为真。

For some reason it looks like 'wp_enqueue_scripts is not working.出于某种原因,它看起来像 'wp_enqueue_scripts 不起作用。 I have tried to also add wp_head() with no luck.我也尝试添加 wp_head() ,但没有运气。

 <?php $GLOBALS['a'] = 'false'; add_action( 'wp_enqueue_scripts', 'load_location' ); function load_location() { $GLOBALS['a'] = 'true'; wp_register_script('load_location_test',get_template_directory_uri().'/load_location.js', array('jquery'),'1.1',true); wp_enqueue_script('load_location_test'); } ?> <?php echo $GLOBALS['a'] ?>;

Thanks in advice谢谢指教

The link is http://www.meetntrain.com/register链接是http://www.meetntrain.com/register

You should look at the docs or at the files of other plugins, walkthroughs online, etc, to understand more.您应该查看文档或其他插件的文件、在线演练等,以了解更多信息。 wp_enqueue_scripts is a hook you're using to load your Javascript, and you have the basic right idea, but try something a little more like this inside your functions.php: wp_enqueue_scripts是你用来加载你的 Javascript 的钩子,你有基本的正确想法,但是在你的 functions.php 中尝试一些更像这样的东西:

function my_custom_scripts() {
    wp_register_script( 'custom-js', 'js/custom.js' );
    wp_enqueue_script( 'custom-js', 'js/custom.js', false );
}

add_action( 'wp_enqueue_scripts', 'my_custom_scripts' );

Change out the "js/custom.js" for wherever your directory has your JS file.为您的 JS 文件所在的目录更改“js/custom.js”。

Another option, if this is proving too tough, is to put a direct link to the JS file at the bottom of your footer.php.另一种选择,如果这被证明太难了,那就是在你的footer.php 的底部放置一个指向JS 文件的直接链接。 Like <script src="/path/to/my/file.js"></script> .<script src="/path/to/my/file.js"></script>

I do find the enqueuing scripts thing a little weird sometimes though, hope this helps you get it figured out.不过,我确实发现排队脚本有时有点奇怪,希望这可以帮助您弄清楚。

So, for your enqueue to work, you need to add the code in your functions.php file or in your plugin.因此,为了让您的入队工作,您需要在您的functions.php文件或插件中添加代码。

add_action( 'wp_enqueue_scripts', 'load_location' );

function load_location() {
    wp_register_script('load_location_test',get_template_directory_uri().'/load_location.js', array('jquery'),'1.1',true);
    wp_enqueue_script('load_location_test');
}

This will add the JS file to all your pages.这会将 JS 文件添加到您的所有页面。 If you want to target a specific page, you can either do it via JavaScript or directly in your PHP file.如果要定位特定页面,可以通过 JavaScript 或直接在 PHP 文件中进行。

If your theme usesbody_class() , you can target that specific page by the class.如果您的主题使用body_class() ,您可以按类定位该特定页面。 You should then wrap your JS like:然后你应该像这样包装你的JS:

if( $('body.classUsed').length ){
// Your JS code here
}

Note that the file will still be enqueued all the time.请注意,该文件仍将一直排队。 Alternately, if you want to add the JS file only to a specified page, you can wrap it in an is_page() condition:或者,如果您只想将 JS 文件添加到指定页面,则可以将其包装在is_page()条件中:

add_action( 'wp_enqueue_scripts', 'load_location' );

function load_location() {
    if( is_page('your-page') ){
       wp_register_script('load_location_test',get_template_directory_uri().'/load_location.js', array('jquery'),'1.1',true);
       wp_enqueue_script('load_location_test');
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM