我正在尝试将一个日期选择器添加到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检查存在的文件?
答案 0 :(得分:0)
如果您想获得主题的本地路径,可以使用get_theme_root()
:
$styleFile = get_theme_root().'/refactored-datepicker/css/ui-lightness/jquery-ui-1.8.17.custom.css';
if(file_exists($styleFile) && $post->post_type == 'show') {
这里的代码是为了防止你想检查文件是否存在 另一台服务器:
<?php function fileExists($path){ return (@fopen($path,"r")==true); } ?>
遗憾的是,file_exists无法访问远程服务器,因此我使用了 fopen功能。