亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

只有第一個“if 函數”派生最新版本的 .js 頁面(wordpress)?

只有第一個“if 函數”派生最新版本的 .js 頁面(wordpress)?

PHP
阿晨1998 2023-08-06 14:40:57
以下用于將最新版本的 .js 頁面加載到 WordPress 頁面上。第一組(資產1)工作正常。但是第二個函數無法加載最新版本的js頁面,只能加載原來的。我希望這兩個函數都能派生最新版本的 .js 頁面,我需要更改什么才能做到這一點?function load_js_assets1() {    if( is_page( 'Example Page 1' ) ) {        $datetime = new DateTime('now');        $revision = $datetime->format("YmdHis");         wp_enqueue_script('example1.js?'.$revision, 'http://website.com/example1.js', array('jquery'), '', false);    }}add_action('wp_enqueue_scripts', 'load_js_assets1');function load_js_assets2() {    if( is_page( 'Example Page 2' ) ) {        $datetime = new DateTime('now');        $revision = $datetime->format("YmdHis");         wp_enqueue_script('example2.js'.$revision, 'http://website.com/example2.js', array('jquery'), '', false);    }}
查看完整描述

2 回答

?
撒科打諢

TA貢獻1934條經驗 獲得超2個贊

夫婦的事情。

  1. 您可以將版本/修訂添加到排隊鉤子本身,因此您不必擔心將版本連接到文件名并意外排除“?”等字符。鉤子會為你做到這一點。

  2. 您缺少第二個函數的 add_action 。

  3. 您應該將兩個 if 語句組合到同一個函數中。

嘗試這樣的事情:

<?php

function load_js_assets() {


    // Declare your revision variables once


    $datetime = new DateTime('now');

    $revision = $datetime->format("YmdHis"); 


    // Test against first statement.  If true enqueue the first script


    if( is_page( 'Example Page 1' ) ) {  

        wp_enqueue_script('example1.js', 'http://website.com/example1.js', array('jquery'), $revision, false);

    }


    // Test against second statement.  If true enqueue the second script


    if( is_page( 'Example Page 2' ) ) { 

        wp_enqueue_script('example2.js', 'http://website.com/example2.js', array('jquery'), $revision, false);

    }


    // Do more if needed...


}

add_action('wp_enqueue_scripts', 'load_js_assets');


查看完整回答
反對 回復 2023-08-06
?
慕虎7371278

TA貢獻1802條經驗 獲得超4個贊

您缺少第二個腳本add_action,這可能是第二個腳本無法正確加載的原因。試試這個代碼,它使用兩個函數,add_action如果您在您希望使用它們的頁面上:


function load_js_assets1() {

   $datetime = new DateTime('now');

   $revision = $datetime->format("YmdHis"); 

   wp_enqueue_script('example1.js?'.$revision, 'http://website.com/example1.js', array('jquery'), '', false);

}


function load_js_assets2() {

   $datetime = new DateTime('now');

   $revision = $datetime->format("YmdHis"); 

   wp_enqueue_script('example2.js'.$revision, 'http://website.com/example2.js', array('jquery'), '', false);

}


if( is_page( 'Example Page 1' ) ) {

    add_action('wp_enqueue_scripts', 'load_js_assets1');

}


if( is_page( 'Example Page 2' ) ) {

    add_action('wp_enqueue_scripts', 'load_js_assets2');

}


查看完整回答
反對 回復 2023-08-06
  • 2 回答
  • 0 關注
  • 111 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號