在wordpress中使用jquery datepicker

tes*_*ter 11 javascript php wordpress jquery datepicker

我希望datepicker能够在我的wordpress模板页面中以某种形式出现,但它不起作用.

这是我的子主题函数的代码.php:

function modify_jquery() {
    if (!is_admin()) {
        // comment out the next two lines to load the local copy of jQuery
        wp_deregister_script('jquery');
        wp_register_script('jquery', 'http://ajax.googleapis.com/ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js', false, '2.1.1');
        wp_enqueue_script('jquery');
    }
}
add_action('init', 'modify_jquery');
function load_jquery_ui_google_cdn() {
    global $wp_scripts;

    wp_enqueue_script('jquery-ui-core');
    wp_enqueue_script('jquery-ui-slider');

    // get the jquery ui object
    $queryui = $wp_scripts->query('jquery-ui-core');

    // load the jquery ui theme
    $urlui = "https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.8/jquery-ui.min.js";
    wp_enqueue_style('jquery-ui-smoothness', $urlui, false, null);
}

add_action('wp_enqueue_scripts', 'load_jquery_ui_google_cdn');
Run Code Online (Sandbox Code Playgroud)

然后我在page-mypage.php中这个:

                <script>
  $(function() {
    $( "#datepicker" ).datepicker();
  });
                </script>
...other code...
Date: <input type="text" id="datepicker">
...other code...
        </form> 
Run Code Online (Sandbox Code Playgroud)

但它没有显示出来.你能帮我解决什么问题吗?

Nat*_*son 41

您用来加载jQuery的代码无效,并且您根本没有加载datepicker(jQuery UI Datepicker).我已经发布了一些关于在WordPress中使用jQuery的正确方法的答案,因此我将提供一个工作示例,然后提供一个链接,如果您想阅读更多内容.

将您插入到子主题functions.php中的代码替换为:

/**
 * Load jQuery datepicker.
 *
 * By using the correct hook you don't need to check `is_admin()` first.
 * If jQuery hasn't already been loaded it will be when we request the
 * datepicker script.
 */
function wpse_enqueue_datepicker() {
    // Load the datepicker script (pre-registered in WordPress).
    wp_enqueue_script( 'jquery-ui-datepicker' );

    // You need styling for the datepicker. For simplicity I've linked to the jQuery UI CSS on a CDN.
    wp_register_style( 'jquery-ui', 'https://code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css' );
    wp_enqueue_style( 'jquery-ui' );  
}
add_action( 'wp_enqueue_scripts', 'wpse_enqueue_datepicker' );
Run Code Online (Sandbox Code Playgroud)

最后将您的用法替换为:

<script>
    jQuery(document).ready(function($) {
        $("#datepicker").datepicker();
    });
</script>
Run Code Online (Sandbox Code Playgroud)

jquery需要单词Jquery而不是$


小智 5

要加载风箱脚本和样式,请在主题functions.php文件中添加风箱代码。

前端脚本

function add_e2_date_picker(){
//jQuery UI date picker file
wp_enqueue_script('jquery-ui-datepicker');
//jQuery UI theme css file
wp_enqueue_style('e2b-admin-ui-css','http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.0/themes/base/jquery-ui.css',false,"1.9.0",false);
}
add_action('wp_enqueue_scripts', 'add_e2_date_picker'); 
Run Code Online (Sandbox Code Playgroud)

后端脚本

    function add_e2_date_picker(){
    //jQuery UI date picker file
    wp_enqueue_script('jquery-ui-datepicker');
    //jQuery UI theme css file
    wp_enqueue_style('e2b-admin-ui-css','http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.0/themes/base/jquery-ui.css',false,"1.9.0",false);
    }
    add_action('admin_enqueue_scripts', 'add_e2_date_picker'); 
Run Code Online (Sandbox Code Playgroud)

只需将这段代码也放到funtions.php文件中,或将这些代码放在下面。

function register_datepiker_submenu() {
    add_submenu_page( 'options-general.php', 'Date Picker', 'Date Picker', 'manage_options', 'date-picker', 'datepiker_submenu_callback' );
}

function datepiker_submenu_callback() { ?>

    <div class="wrap">

    <input type="text" class="datepicker" name="datepicker" value=""/>

    </div>

    <script>
    jQuery(function() {
        jQuery( ".datepicker" ).datepicker({
            dateFormat : "dd-mm-yy"
        });
    });
    </script> 

<?php }
add_action('admin_menu', 'register_datepiker_submenu');
?>
Run Code Online (Sandbox Code Playgroud)

添加此代码后,您将在Admin Menu-> Settigns-> Date Picker中获得一个日期选择

请查看有关将jQuery DatePicker添加到WordPress主题或插件的详细信息