【发布时间】:2016-10-14 14:01:15
【问题描述】:
我已设法将属性 target="_blank" 添加到我的外部产品页面,但我似乎无法更改父(分组产品)页面上的链接。
我可以通过修改 external.php 并将标签添加到实际链接本身来做到这一点。
<p class="cart">
<?php sdnetwork(); sdcondition(); parent_permalink_button(); ?><a href="<?php echo esc_url( $product_url ); ?>" target="_blank" rel="nofollow" class="single_add_to_cart_button button alt"><img src="/wp-content/themes/wootique-child/images/icons/new_tab_icon.gif" alt="Opens in New Tab"> <?php echo esc_html( $button_text ); ?></a>
</p>
如何更改分组产品父页面上的链接添加此属性,我首先想到的是修改grouped.php但链接生成方式不同。
<?php woocommerce_template_loop_add_to_cart(); ?>
我怎样才能将我的标签添加到上面生成的链接中?我曾考虑过使用钩子,但我需要一些帮助。
编辑:
只是想知道我是否可以像这样使用 jQuery .....
jQuery(document).ready(function($) {
$(".button.product_type_external").each(function() {
$(this).find("a").attr("target", "_blank");
});
});
问题是大多数链接在页面加载时都被隐藏了,我担心这可能会占用大量资源,还是会这样吗?对 jQuery 很陌生。
http://mobilereactor.co.uk/shop/mobile-phones/sony-xperia-z5-compact-coral-deals/
编辑由于 cal_b 解决了:
add_filter( 'woocommerce_loop_add_to_cart_link', 'add_target_blank', 10, 2 );
function add_target_blank( $link, $product ){
global $post;
$product = get_product( $post->ID );
if( $product->is_type( 'external' ) ){
// I simply added target="_blank" in the line below
$link = sprintf( '<a rel="nofollow" href="%s" target="_blank" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>',
esc_url( $product->add_to_cart_url() ),
esc_attr( isset( $quantity ) ? $quantity : 1 ),
esc_attr( $product->id ),
esc_attr( $product->get_sku() ),
esc_attr( isset( $class ) ? $class : 'button' ),
esc_html( $product->add_to_cart_text() )
);
return $link;
} else {
// I simply remove target="_blank" in the line below
$link = sprintf( '<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>',
esc_url( $product->add_to_cart_url() ),
esc_attr( isset( $quantity ) ? $quantity : 1 ),
esc_attr( $product->id ),
esc_attr( $product->get_sku() ),
esc_attr( isset( $class ) ? $class : 'button' ),
esc_html( $product->add_to_cart_text() )
);
return $link;
}
}
【问题讨论】:
-
不要使用 jQuery。有一个模板可以让您覆盖链接,就像您的
external.php一样。您已经彻底修改了您链接到的模板,因此您需要为我们提供更多工作。我们在您上面的链接中查看的是哪个模板? -
在上面的链接中,它是 grouped.php 的修改版本
-
我认为链接是由
<?php woocommerce_template_loop_add_to_cart(); ?>在grouped.php中生成的,虽然不知道如何修改它?
标签: php jquery wordpress woocommerce hook