繁体   English   中英

我如何将这个jquery代码包含到wordpress中?

[英]How would I include this jquery code to wordpress?

数周以来,我一直在寻找解决方案。 如何在wordpress中包含一个jquery ...所以我用过wp_enqeue_scripts(); 但是我还是不明白。 因此,例如,我有此jQuery向下滑动代码。

$jQuery(document).ready(function() {

    $jQuery('#slide').slideDown();  

});

现在,此代码可完美地在我的静态html代码上正常工作,该代码中附带幻灯片ID。

那么我将如何将该幻灯片包含到我的wordpress插件中? 这是我的插件代码

<?php
/*
Plugin Name: Jquery Test.
Plugin URI: http://wsplugins.com
Description: A plugin that increases your website traffic by floating facebook, twitter and google + share button. The buttons bounce and move over your page ensuring maximum attention by your visitors.
Author: Ronny Kibet.
Author URI: http://@.com
Version: 1.0
 */

谢谢。

要使脚本在WordPress中排队,请使用以下命令:

/**
* Proper way to enqueue scripts
*/
function theme_name_scripts() {
    wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/example.js',    array('dependencyScriptThatNeedsToExecuteBeforeMyScriptName','forExample','jquery), '1.0.0', true );
}

add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );

我也在寻找相同的解决方案,但我自己找到了。

在js导演中创建一个新文件custom-functions.js,然后越过您要排队的代码。 现在将其放入functions.php

    wp_enqueue_script( 'custom-functions', get_template_directory_uri() . '/js/custom-functions.js', array(), '1.0', true );

阅读Bootstrap WordPress代码段上的完整教程-wp_enqueue_script

function theme_name_scripts() {
        wp_enqueue_script( 'script-name',  plugins_url( '/js/responsiveslides.min.js' , __FILE__ ),      array(),false, true );
}
add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );

您有两种选择。

选项1

将代码粘贴到footer.php(或header.php)中。

<script>
$(function() {
    $jQuery('#slide').slideDown();  
});
</script>

这将导致在页面加载时将ID为“ slide”的每个DOM元素都向下滑动。

选项#2

使用WordPress的API ,您可以在主题中创建一个方法,以在其中包含代码的JavaScript文件中添加该方法。 这更加复杂,需要了解WordPress挂钩。

我遇到了同样的问题,但我得到了“某种程度”的帮助。 首先,您需要使用带有脚本名称的wp_register_scripts,然后执行wp_enque_script(“ scriptname”); ...我不明白为什么会这样,但是我们只需要处理:-/

您可能需要使用noConflict,例如:

<script type="text/javascript">

var slide=jQuery.noConflict();

slide(function() {
    slide('#slide').slideDown();  
});
</script>

暂无
暂无

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

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