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

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

在 if 語句中調用腳本 WordPress

在 if 語句中調用腳本 WordPress

PHP
白板的微信 2023-07-01 15:12:25
我正在嘗試使用 Polylang 有條件地根據 WordPress 中的語言調用腳本。我可以在 Google Inspector 中看到該腳本,但它不起作用。腳本在定制器中正常工作。代碼:<?php if(pll_current_language() == 'en') : ?><script type="text/javascript">    const cartBtn = document.querySelector('.cart button');const formCart = document.querySelector('div.product.elementor form.cart');var newBtn = document.createElement('a');newBtn.innerHTML = "<h1>Back to shop</h1>";newBtn.classList.add('cart-custom-link');newBtn.setAttribute("href", "/shop/");cartBtn.addEventListener('click', function() {   formCart.appendChild(newBtn);   console.log('click');});</script><?php endif; ?><?php if(pll_current_language() == 'uk') : ?><script type="text/javascript">    const cartBtn = document.querySelector('.cart button');const formCart = document.querySelector('div.product.elementor form.cart');var newBtn = document.createElement('a');newBtn.innerHTML = "<h1>Повернутися до магазину</h1>";newBtn.classList.add('cart-custom-link');newBtn.setAttribute("href", "/shop-uk/");cartBtn.addEventListener('click', function() {   formCart.appendChild(newBtn);   console.log('click');});</script><?php endif; ?>有什么解決辦法嗎?
查看完整描述

1 回答

?
慕萊塢森

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

我的假設是代碼不起作用是因為腳本代碼是在 DOM 樹準備好之前添加(并因此運行)的。因此,它必須被包裝在一個window.onload處理程序(或 jQuery 的$(document).ready();)中。另外,為每種語言復制并粘貼 JS 代碼也不是很漂亮。有一個更干凈的解決方案:

  • 將代碼放入 .js 文件中

  • 使用 JS 對象來翻譯文本

  • 將腳本排隊,然后使用wp_localize_script()

像這樣: my_cart.js

window.onload = function () {

  const cartBtn = document.querySelector('.cart button');

  const formCart = document.querySelector('div.product.elementor form.cart');

  var newBtn = document.createElement('a');

  newBtn.innerHTML = "<h1>"+cart_localize.back_to_shop+"</h1>";

  newBtn.classList.add('cart-custom-link');

  newBtn.setAttribute("href", "/shop-uk/");


  cartBtn.addEventListener('click', function() {

    formCart.appendChild(newBtn);

    console.log('click');

  });

}

接下來,在 PHP 中,將腳本排入隊列,如下所示:


function load_localized_scripts() {

  $cart_localize = array(

    'back_to_shop' => 'Back to shop', // default

  );

  if (pll_current_language() == 'uk') {

    $cart_localize['back_to_shop'] = 'Повернутися до магазину';

  }

  if (pll_current_language() == 'de') {

    $cart_localize['back_to_shop'] = 'Zurück zum Shop';

  }

  wp_enqueue_script('my-cart', plugin_dir_url( __FILE__ ) . 'js/my_cart.js');

  wp_localize_script('my-cart', 'cart_localize', $cart_localize); 

}

add_action('wp_enqueue_scripts', 'load_localized_scripts');

該$cart_localize數組可以包含任意數量的標簽鍵→值對=>文本翻譯。它被插入到名為函數第二個參數的 JavaScript 對象中wp_localized_script。然后,您可以使用 JS 在 JS 中訪問它cart_localize.key_name。


pll_register_string從技術上講,您還可以使用命名注冊 Polylang 字符串back_to_shop,并使用以下函數輕松插入您在語言 → 字符串翻譯下輸入的翻譯pll__():


    $cart_localize['back_to_shop'] = pll__('back_to_shop');

我不會在這里完全介紹這一點,因為我不確定這是否符合您想要管理翻譯的方式。


查看完整回答
反對 回復 2023-07-01
  • 1 回答
  • 0 關注
  • 147 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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