【发布时间】:2012-01-11 01:12:19
【问题描述】:
我正在尝试将日期选择器添加到 wordpress 中的自定义元框。如果我像这样将脚本排入队列
// add stylesheets for date picker
add_action('admin_print_styles', 'add_datepicker_styles');
function add_datepicker_styles() {
global $post;
$styleFile = get_bloginfo("template_url").'/refactored-datepicker/css/ui-lightness/jquery-ui-1.8.17.custom.css';
if(file_exists($styleFile) && $post->post_type == 'show') {
wp_enqueue_style("datepicker-css", $styleFile);
}
}
// add javascript for date picker
add_action('admin_print_scripts', 'add_datepicker_js');
function add_datepicker_js() {
global $post;
$jsFile = get_bloginfo("template_url").'/refactored-datepicker/js/jquery-ui-1.8.17.custom.min.js';
if(file_exists($jsFile) && $post->post_type == 'show') {
wp_enqueue_script("datepicker-js", $jsFile);
}
}
// add date picker init
add_action('admin_head', 'add_datepicker_init');
function add_datepicker_init() {
global $post;
if($post->post_type == 'show') {
echo "<script type='text/javascript'>jQuery(document).ready(function() { jQuery('#show_date').datepicker(); });</script>";
}
}
我收到一个错误,即 jQuery('#show_date').datepicker();不是函数。当我检查处理后的源代码时,我看到默认情况下会加载 jQuery,但根本没有加载样式和 jQuery UI 脚本。附加到 admin_head 钩子的代码加载正常,如果我在此函数中回显脚本和样式,它会按照我想要的方式工作。
// add date picker init
add_action('admin_head', 'add_datepicker_init');
function add_datepicker_init() {
global $post;
if($post->post_type == 'show') {
echo "<link rel='stylesheet' type='text/css' href='".get_bloginfo("template_url").'/refactored-datepicker/css/ui-lightness/jquery-ui-1.8.17.custom.css'."' />";
echo "<script type='text/javascript' src='".get_bloginfo('template_url').'/refactored-datepicker/js/jquery-ui-1.8.17.custom.min.js'."'></script>";
echo "<script type='text/javascript'>jQuery(document).ready(function() { jQuery('#show_date').datepicker(); });</script>";
}
}
那么是 wp_enqueue_style/script() 函数还是 add_action 钩子有问题?我是 wordpress 的新手,所以我不确定如何解决这个问题。我已经进行了谷歌搜索,无论我走到哪里,代码看起来都像我的一样。此外,如果您想知道文件的路径是否正确。
谁能想到我的问题的解决方案?只是回显脚本和样式是错误的,还是应该将它们排入队列?
谢谢!
编辑:
file_exists($jsFile) 返回 false。如果我删除了对文件的条件检查,代码就可以工作了。但是为什么呢?您如何使用以 http:// 开头的 url 而不是本地文件路径来检查存在的文件?
【问题讨论】:
标签: wordpress jquery-ui datepicker