Woocommerce-外部/附属产品图片和外部链接的标题(“新建”标签) [英] Woocommerce - External/Affiliate Product Image and title to External Link (New tab)
问题描述
我从使用wordpress和woocommerce进行编程开始。在搜索Stack之后,我需要帮助。
I'm starting with programming with wordpress and woocommerce. I need help after searching for Stack.
我需要我商店的产品图像和标题以指向会员链接,而无需浏览单个帖子页。一切都会在新选项卡中打开。
I need the images and titles of the products of my store to point to the affiliate link without going through the single post page. And that everything opens in a new tab.
跟随此线程: Woocommerce-外部/关联产品图像到外部链接(购买URL)
我使用了Sadoo的Edit#2代码,对我来说非常有用。
I have used the Edit # 2 code from Sadoo and it works perfectly for me.
remove_action('woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open');
add_action('woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_link_open', 15);
add_action('woocommerce_before_shop_loop_item', 'woocommerce_add_aff_link_open', 10);
add_action('woocommerce_before_shop_loop_item_title', 'woocommerce_add_aff_link_close', 10);
function woocommerce_add_aff_link_open(){
$product = wc_get_product(get_the_ID());
if( $product->is_type( 'external' ) )
echo '<a href="' . $product->get_product_url() . '" class="woocommerce-LoopProductImage-link">';
}
function woocommerce_add_aff_link_close(){
$product = wc_get_product(get_the_ID());
if( $product->is_type( 'external' ) )
echo '</a>';
}
我只需要标题就可以链接图片,并且所有内容都会在新标签页中打开。
I just need the title to link like the image and everything opens in new tabs.
有人可以说我要继续吗?
Can someone say how can I continue?
谢谢
推荐答案
如果要在商店页面中为外部产品的新标签中打开附加到图像的链接,请像这样编辑代码
If you want to open link attached to image in a new tab for a external products in your shop page then edit your code like these
remove_action('woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open');
add_action('woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_link_open', 15);
add_action('woocommerce_before_shop_loop_item', 'woocommerce_add_aff_link_open', 10);
add_action('woocommerce_before_shop_loop_item_title', 'woocommerce_add_aff_link_close', 10);
function woocommerce_add_aff_link_open(){
$product = wc_get_product(get_the_ID());
if( $product->is_type( 'external' ) ) {
echo '<a target="_blank" href="' . $product->get_product_url() . '" class="">';
}
}
function woocommerce_add_aff_link_close(){
$product = wc_get_product(get_the_ID());
if( $product->is_type( 'external' ) ) {
echo '</a>';
}
}
然后,如果您希望标题将以新的名称打开标签,而不是将这些覆盖的woocommerce功能也添加到主题的functions.php文件中。
Then if you want title will be opened in a new tab with external link than add these overridden woocommerce function to your functions.php file of your theme too
function woocommerce_template_loop_product_link_open() {
global $product;
if( $product->is_type( 'external' ) ) {
$link = apply_filters( 'woocommerce_loop_product_link', $product->get_product_url(), $product );
echo '<a target="_blank" href="' . esc_url( $link ) . '" class="woocommerce-LoopProduct-link woocommerce-loop-product__link">';
} else {
$link = apply_filters( 'woocommerce_loop_product_link', get_the_permalink(), $product );
echo '<a href="' . esc_url( $link ) . '" class="woocommerce-LoopProduct-link woocommerce-loop-product__link">';
}
}
这篇关于Woocommerce-外部/附属产品图片和外部链接的标题(“新建”标签)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!