更改woocommerce标题挂钩时,第一项保持不变 [英] When changing woocommerce title hook the first item doesn't change

查看:66
本文介绍了更改woocommerce标题挂钩时,第一项保持不变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种我不理解的奇怪行为

I have a strange behaviour that I don't understand

我更改了woocommerce_shop_loop_item_title挂钩,以添加指向产品标题的链接.这是我在functions.php中的代码

I've changed the woocommerce_shop_loop_item_title hook to add a link to the title of the product. This is my code inside functions.php

// Add HREF TO TITLE
function abChangeProductsTitleHook(){
    remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10 );
    add_action('woocommerce_shop_loop_item_title', 'abChangeProductsTitle', 10 );
}
add_action( 'woocommerce_shop_loop_item_title', 'abChangeProductsTitleHook' );
function abChangeProductsTitle() {
    echo '<h2 class="woocommerce-loop-product_title"><a href="'.get_the_permalink().'">' . get_the_title() . '</a></h2>';
}

除了第一个产品,它在所有产品上均能完美运行.

It works perfectly on all the products except the first one.

我还对另一个挂钩进行了类似的更改,以将缩略图图像更改为背景图像,而且该功能不适用于第一个产品.即使更改产品顺序,它始终是第一款产品.

I've also made a similar change to another hook to change the thumbnail image to a background image and also this one doesn't work on the first product. It's always the first product even if I change the order of the products.

下面您将看到页面上第一行产品的屏幕截图,并且第一行的显示方式不同

Below you see a screenshot of the the first row of products on the page and that the first one is displayed differently

如果有人知道这个问题或可以向我指出正确的方向,那将非常有帮助.

It would be really helpful if anyone knows that problem or can point me in the right direction .

非常感谢你 亚历克斯

推荐答案

删除和添加woocommerce_shop_loop_item_title的方式就是问题所在.尝试这种方式.

The way you are removing and adding the woocommerce_shop_loop_item_title is the problem. Try it this way.

remove_action( 'woocommerce_shop_loop_item_title','woocommerce_template_loop_product_title', 10 );
add_action('woocommerce_shop_loop_item_title', 'abChangeProductsTitle', 10 );
function abChangeProductsTitle() {
    echo '<h2 class="woocommerce-loop-product_title"><a href="'.get_the_permalink().'">' . get_the_title() . '</a></h2>';
}

这篇关于更改woocommerce标题挂钩时,第一项保持不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆