在WordPress中使用jQuery Datepicker [英] Use jquery datepicker in wordpress
问题描述
我希望在我的wordpress模板页面中的表单中与谁约会,但这是行不通的.
I want datepicker to who in a form in my wordpress template page, but it doesn't work.
这是我拥有子主题functions.php的代码:
This is the code I've the child theme functions.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');
然后我在page-mypage.php中找到了
Then I've this in page-mypage.php:
<script>
$(function() {
$( "#datepicker" ).datepicker();
});
</script>
...other code...
Date: <input type="text" id="datepicker">
...other code...
</form>
但是没有显示.你能帮我解决问题吗?
But it doesn't show. Could you help me to wind what's wrong?
推荐答案
您用于加载jQuery的代码无效,并且您根本没有加载datepicker(jQuery UI Datepicker).我已经发布了一些有关在WordPress中使用jQuery的正确方法的答案,因此,如果您想了解更多信息,我将提供一个工作示例,然后提供一个链接.
The code you're using to load jQuery is invalid and you aren't loading datepicker (jQuery UI Datepicker) at all. I've posted a few answers regarding the correct way to use jQuery in WordPress so I'll provide a working example and then a link if you'd like to read more.
将您插入到子主题functions.php中的代码替换为:
Replace the code you've inserted into your child theme functions.php with:
/**
* 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' );
最后将您的用法替换为:
Finally replace your usage with:
<script>
jQuery(document).ready(function($) {
$("#datepicker").datepicker();
});
</script>
这篇关于在WordPress中使用jQuery Datepicker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!