Php 如果文件存在,将脚本排队
我与wordpress合作处理我的孩子主题 我的站点已安装到Php 如果文件存在,将脚本排队,php,jquery,wordpress,Php,Jquery,Wordpress,我与wordpress合作处理我的孩子主题 我的站点已安装到mydomain.xx/install,但运行于mydomain.xx 我的functions.php工作时如下所示: <?php /* Script in head */ function carica_scripts() { /* Common scripts */ // insert scripts here, if some /* Page-based script */ $pageId = get_
mydomain.xx/install
,但运行于mydomain.xx
我的functions.php
工作时如下所示:
<?php
/* Script in head */
function carica_scripts() {
/* Common scripts */
// insert scripts here, if some
/* Page-based script */
$pageId = get_the_ID();
$pageType = get_post_type();
$myBaseURL = get_stylesheet_directory_uri() . '/js/';
/* Page-type scripts */
if($pageType == "product") {
wp_enqueue_script('CondAll', $myBaseURL . 'CondAll.js', array('jquery'));
wp_enqueue_script('CondShipping', $myBaseURL . 'CondShipping.js', array('jquery'));
}
/* Page-id scripts */
if($pageId == "1") {
wp_enqueue_script('Cond1', $myBaseURL . 'Cond1.js', array('jquery'));
}
if($pageId == "294") {
wp_enqueue_script('Cond294', $myBaseURL . 'Cond294.js', array('jquery'));
}
if($pageId == "318") {
wp_enqueue_script('Cond318', $myBaseURL . 'Cond318.js', array('jquery'));
}
if($pageId == "232") {
wp_enqueue_script('Cond232', $myBaseURL . 'Cond232.js', array('jquery'));
}
/* END of page-based script */
}
add_action( 'wp_enqueue_scripts', 'carica_scripts' );
?>
我想要实现的是避免使用$pageId
上的所有if,如果相关文件存在于我的子主题的子目录/js/
中,则在pageId XXX
上自动将jQuery脚本CodXXX.js
排队。文件存在()
需要文件的绝对路径,而不是URL
使用get\u stylesheet\u directory()
获取所需的路径。另外,get\u不应在循环之外使用\u ID()
例如:
/*
* Enqueue CondXXX.js on page XXX if file CondXXX.js exists
*/
function carica_scripts() {
global $post;
// Check we're on a page.
if ( ! is_page() ) {
return false;
}
// Build the filename to check.
$handle = 'Cond' . $post->ID;
$relpath = '/js/' . $handle . '.js';
// Get path + url to file.
$file_path = get_stylesheet_directory() . $relpath;
$file_url = get_stylesheet_directory_uri() . $relpath;
if ( file_exists( $file_path ) ) {
wp_enqueue_script( $handle, $file_url, array( 'jquery' ) );
}
}
add_action( 'wp_enqueue_scripts', 'carica_scripts' );
Nathan Dawson给出的答案在我的情况下不起作用,我不得不做一些变通。我最终得到了以下代码:
/* Load scripts in head */
function carica_scripts() {
/* Common scripts */
/* END of common scripts */
/* Page-based script */
$pageId = get_the_ID();
$pageType = get_post_type();
$handle = 'Cond' . $pageId;
$file = $handle .'.js';
$relPath = '/js/';
$styleSheet_path = get_stylesheet_directory();
$domain_base = 'mydomain.it/public_html/';
$start_pos = strpos ( $styleSheet_path, $domain_base) + strlen ($domain_base);
$basePath = substr ( $styleSheet_path, $start_pos); // I need everything after 'mydomain.it/public_html/'
$file_path = $basePath. $relPath . $file;
$enqueue_path = get_stylesheet_directory_uri() . $relPath;
$enqueue_file = $enqueue_path . $file;
/* if page-type is ... (product, in this case) */
if($pageType == "product") {
wp_enqueue_script('CondAll', $enqueue_path . 'CondAll.js', array('jquery'));
wp_enqueue_script('CondShipping', $enqueue_path . 'CondShipping.js', array('jquery'));
}
/* If page id is... (1, in this case - because page 1 is not product but I want CondAll here too) */
if($pageId == "1") {
wp_enqueue_script('CondAll', $enqueue_path . 'CondAll.js', array('jquery'));
}
/* auto load script CondXXX.js from subdir js/ if file exists */
if ( file_exists( $file_path ) ) {
wp_enqueue_script( $handle, $enqueue_file, array( 'jquery' ) );
}
/* END of page-based script */
}
add_action( 'wp_enqueue_scripts', 'carica_scripts' );
现在脚本加载。我尝试了你的代码,但仍然不起作用。。。顺便说一句,我正在使用一个子主题并将代码插入到它的functions.php中,
get\u ID()
以及get\u post\u type()
在我看来工作正常。但我使用了你的完整代码以防万一……我想不出任何理由,如果它得到了正确的实现,它为什么会不起作用。我可能会回显$file\u url的结果,并确保它调用的文件存在。您还提到了get_post_type()
-是否希望将此文件加载到自定义的post类型而不是页面上?$relpath='/js/'$处理。js′;从目录函数返回的路径没有尾随斜杠。@bobdye不确定您的意思?我用来获取路径的函数不包含尾随斜杠,这就是我在relpath变量中添加它的原因。get_stylesheet_directory()返回类似“/var/www/html/wp content/themes/yourtheme”的内容。使用原始答案中的$relpath,您将得到“/var/www/html/wp-content/themes/yourthemejs/condxxx.js”。你的主题后面少了一条斜线。我找到了解决办法。Nathan Dawson回答中提供的代码在我的情况下不起作用(可能是我的提供商的错)。我必须做一些改变,我将用我找到的解决方案更新我的问题。