Woocommerce - 外部/联属产品外部链接的图像和标题(新标签)

问题描述 投票:2回答:2

我开始使用wordpress和woocommerce进行编程。搜索Stack后我需要帮助。

我需要我的商店产品的图像和标题指向联盟链接,而不通过单个帖子页面。并且所有内容都会在新标签中打开。

关注此主题:Woocommerce - External/Affiliate Product Image to External Link (Buy url)

我使用了Sadoo的Edit#2代码,它对我来说非常适合。

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

我只需要像图像一样链接标题,所有内容都在新标签页中打开。

谁能说我怎么能继续?

谢谢

IMAGE

php wordpress woocommerce affiliate woothemes
2个回答
1
投票

如果要在商店页面中的外部产品的新选项卡中打开附加到图像的链接,请编辑这些代码

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文件中

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

1
投票

以下是一种更简洁的方法,可以将target="_blank"添加到存档页面上的链接,以便在新选项卡中打开它们:

function ns_open_in_new_tab($args, $product) 
{
    if( $product->is_type('external') ) {
        // Inject target="_blank" into the attributes array
        $args['attributes']['target'] = '_blank';
    }    

    return $args;
}
add_filter( 'woocommerce_loop_add_to_cart_args', 'ns_open_in_new_tab', 10, 2 );

用您自己的命名空间缩写替换ns_部分。

© www.soinside.com 2019 - 2024. All rights reserved.