Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
344 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 Answer

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');
   });
  });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...