![](/img/trans.png)
[英]WooCommerce - external/affiliate product title and Image on archive page to external link (New Tab)
[英]Woocommerce - External/Affiliate Product Image and title to External Link (New tab)
我開始使用 wordpress 和 woocommerce 進行編程。 搜索 Stack 后我需要幫助。
我需要我商店產品的圖片和標題指向附屬鏈接,而無需通過單個帖子頁面。 並且所有內容都在新選項卡中打開。
一直在關注這個線程: Woocommerce - 外部/附屬產品圖片到外部鏈接(購買網址)
我使用了來自 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>';
}
我只需要標題像圖像一樣鏈接,所有內容都會在新標簽中打開。
有人可以說我該如何繼續?
謝謝
如果您想在商店頁面中的外部產品的新選項卡中打開附加到圖像的鏈接,請像這樣編輯您的代碼
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">';
}
}
這是一種更簡潔的方法,用於將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_
部分替換為您自己的命名空間縮寫。
有沒有人將此應用於 FLATSOME 主題? 我在functions.php文件中嘗試了一些更改但沒有結果,請幫忙! 謝謝你
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.