[英]Add target="_blank" to get_categories() WordPress code
我需要將target="_blank"
添加到我在 WordPress+WooCommerce 安裝上使用此 PHP 代碼檢索的鏈接:
$products = wc_get_products([
'status' => 'publish',
'limit' => -1,
'product_cat' => 'talleres'
]);
$locaciones = array_map(function ($product) {
return [
// more code goes here, I just deleted it for this question
'popup_html' => $product->get_categories() // this is where I need the target="_blank"
];
}, $products);
我嘗試使用 jQuery:
$(document).ready( function(){
$('a').attr('target', '_blank');
});
但它不起作用,它不會將target="_blank"
添加到鏈接中。
我在想也許這可以直接添加到 PHP 代碼中?
你可以使用這個腳本。 您需要添加 Anchor 標簽的類或父 div,否則它將添加到所有錨點。
jQuery(document).ready( function($){
$('a').each(function(){
$(this).attr('target', '_blank');
});
});
您可以使用preg_replace之類的 php 正則表達式過濾器並通過“href”搜索並替換匹配項,將 target="_blank" 放在 href 之前導致“target='_blank' href”
由於我需要在新選項卡上打開所有鏈接,因此我只對整個頁面使用了<base target="_blank">
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.