programing

WordPress 테마에 jQuery를 포함하려면 어떻게 해야 합니까?

subpage 2023. 3. 15. 19:36
반응형

WordPress 테마에 jQuery를 포함하려면 어떻게 해야 합니까?

WordPress는 처음이라서 jQuery를 테마에 포함시키는 방법을 찾고 있습니다.

저는 다음과 같은 기능을 함수로 만듭니다.php 테마:

function load_java_scripts() {
    // Load FlexSlider JavaScript that handle the SlideShow:
    wp_enqueue_script('jQuery-js', 'http://code.jquery.com/jquery.js', array(), '1.0', true);
}
add_action('wp_enqueue_scripts', 'load_java_scripts');

그래서 다른 JavaScript 또는 CSS 로컬리소스로 추가할 수 있다고 생각합니다만, 이 경우 jquery.js는 로컬리소스가 아니라 온라인리소스이기 때문에 이 방법은 잘 모르겠습니다(같은 것입니까).

온라인 검색에서 제 테마에 jQuery를 추가하는 다른 방법이 발견되었기 때문에 의문도 있습니다.

이 작업을 올바르게 완료하는 방법에 대해 몇 가지 정보를 주시겠습니까?

WordPress에 있는 jQuery를 사용하지 않는 특별한 이유가 있습니까?

jQuery에 의존하는 JavaScript 파일을 추가해야 할 경우 jQuery를 종속성으로 추가할 수 있습니다.

<?php

function my_scripts_method() {
    wp_enqueue_script(
        'custom-script',
        get_stylesheet_directory_uri() . '/js/custom_script.js', #your JS file
        array( 'jquery' ) #dependencies
    );
}

add_action( 'wp_enqueue_scripts', 'my_scripts_method' );

?>

WordPress는 jQuery를 충돌하지 않는 래퍼로 로드합니다.따라서 코드는 다음과 같습니다.

jQuery(document).ready(function($) {
    // Inside of this function, $() will work as an alias for jQuery()
    // and other libraries also using $ will not be accessible under this shortcut
});

WP에는 이미 jQuery가 포함되어 있기 때문에, 당신의 테마에 맞게 로드하고, 이렇게 당신의 기능에 추가합니다.php

function load_scripts(){
    //Load scripts:
    wp_enqueue_script('jquery'); # Loading the WordPress bundled jQuery version.
    //may add more scripts to load like jquery-ui
}
add_action('wp_enqueue_scripts', 'load_scripts');

jQuery를 테마에 포함시키는 방법은 여러 가지가 있습니다.저는 항상 WP 번들 버전을 사용하고 있는데, 매우 심플합니다.

Word에서 No need for Custom Jquery를 누릅니다.종속성을 'jquery'로 추가하면 자동으로 로드됩니다.

이거 먹어봐

<?php
    function load_external_jQuery() {
        wp_deregister_script( 'jquery' ); // deregisters the default WordPress jQuery 
        $url = 'http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js'; // the URL to check against  
        $test_url = @fopen($url,'r'); // test parameters  
        if( $test_url !== false ) { // test if the URL exists if exists then register the external file  
            wp_register_script('jquery', '//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js');
        }
        else{// register the local file 
            wp_register_script('jquery', get_template_directory_uri().'/js/jquery.js', __FILE__, false, '1.7.2', true);  
        }
        wp_enqueue_script('jquery'); // enqueue the jquery here
    }
    add_action('wp_enqueue_scripts', 'load_local_jQuery'); // initiate the function 
?>

다음 방법을 사용하여 jQuery를 포함할 수 있습니다.

wp_enqueue_script( 'jquery' );  

wp_enqueue_script( 'load-js-validate', 'foldername/jquery.js' );

Directly add in header file.<script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/js/jquery.js"></script>

function js_scripts() {
wp_enqueue_script( 'jquery', get_template_directory_uri() . '/js/example.js');
}

add_action( 'wp_enqueue_scripts', 'js_scripts' ); // add this in function file

언급URL : https://stackoverflow.com/questions/21256679/how-to-include-jquery-in-a-wordpress-theme

반응형