Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
461 views
in Technique[技术] by (71.8m points)

php - 将target =“ _ blank”添加到get_categories()WordPress代码(Add target=“_blank” to get_categories() WordPress code)

I need to add target="_blank" to links I'm retrieving using this PHP code on a WordPress+WooCommerce installation:

(我需要在WordPress + WooCommerce安装中使用此PHP代码的链接中添加target="_blank" :)

$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);

I tried with jQuery:

(我尝试使用jQuery:)

  $(document).ready( function(){
    $('a').attr('target', '_blank');
  });

But it is not working, it doesn't add the target="_blank" to the link.

(但是它不起作用,它没有将target="_blank"到链接中。)

I'm thinking maybe this can be added directly to the PHP code?

(我在想,这可以直接添加到PHP代码中吗?)

  ask by leandroprz translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You may use this script.

(您可以使用此脚本。)

you need to add class or parent div of Anchor tag otherwise it will add to all anchors.

(您需要添加Anchor标记的类或父div,否则它将添加到所有锚。)

jQuery(document).ready( function($){
    $('a').each(function(){
       $(this).attr('target', '_blank');
   });
  });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...