简体   繁体   English

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

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

I have been looking to figure this out for weeks. 数周以来,我一直在寻找解决方案。 How to include a jquery in wordpress... so I have used the wp_enqeue_scripts(); 如何在wordpress中包含一个jquery ...所以我用过wp_enqeue_scripts(); but then I still don't understand it. 但是我还是不明白。 so for example I have this jquery slide down code.. 因此,例如,我有此jQuery向下滑动代码。

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

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

});

now this code works perfectly on my static html code which has the slide ID in it by the way.. 现在,此代码可完美地在我的静态html代码上正常工作,该代码中附带幻灯片ID。

so how would i include that slide to my wordpress plugin? 那么我将如何将该幻灯片包含到我的wordpress插件中? here is my plugin code 这是我的插件代码

<?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
 */

thanks. 谢谢。

to enqueue a script in wordpress in a plugin use this: 要使脚本在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' );

I was also searching for the same solution but I found it myself. 我也在寻找相同的解决方案,但我自己找到了。

create a new file custom-functions.js in the js director and past the code that you want to enqueue in that file. 在js导演中创建一个新文件custom-functions.js,然后越过您要排队的代码。 Now enqueue it in the functions.php 现在将其放入functions.php

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

read the full tutorial at Bootstrap WordPress Snippets - wp_enqueue_script 阅读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' );

You have a couple of options. 您有两种选择。

Option #1 选项1

Paste the code into footer.php (or header.php). 将代码粘贴到footer.php(或header.php)中。

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

This would result in every DOM element with an ID of "slide" to slideDown upon page load. 这将导致在页面加载时将ID为“ slide”的每个DOM元素都向下滑动。

Option #2 选项#2

Using WordPress' API , you could create a method in your theme to include a JavaScript file with the code inside . 使用WordPress的API ,您可以在主题中创建一个方法,以在其中包含代码的JavaScript文件中添加该方法。 This is more complicated and requires an understanding of WordPress hooks. 这更加复杂,需要了解WordPress挂钩。

I am having this same problem, but I got it to "somewhat" work. 我遇到了同样的问题,但我得到了“某种程度”的帮助。 First you need to use wp_register_scripts, with the script name, and then do wp_enque_script("scriptname");... I don't understand why this is the way it is, but we just have to deal :-/ 首先,您需要使用带有脚本名称的wp_register_scripts,然后执行wp_enque_script(“ scriptname”); ...我不明白为什么会这样,但是我们只需要处理:-/

you may need to use noConflict, an example would be: 您可能需要使用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