我有一种奇怪的行为,我不明白
我已更改了woocommerce_shop_loop_item_title挂钩,以添加指向产品标题的链接。这是我在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>';
}
除了第一个产品外,它完美适用于所有产品。
我也对另一个钩子进行了类似的更改,将缩略图图像更改为背景图像,而且这个也不适用于第一个产品。即使我改变了产品的顺序,它也始终是第一个产品。
下面是页面上第一行产品的屏幕截图,第一行产品的显示方式不同
如果有人知道这个问题或者能指出我正确的方向,那将会非常有帮助。
非常感谢你 亚历
答案 0 :(得分:8)
您要删除和添加woocommerce_shop_loop_item_title
的方式是问题所在。试试这种方式。
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>';
}
答案 1 :(得分:0)
虽然已接受的答案有效,但为什么我们必须remove
标题,然后再次add
将其重新输入?我们可以一开始就简单地filter
。
add_filter('the_title', 'your_shop_custom_title', 20, 2);
function your_shop_custom_title($the_title, $id)
{
if (is_shop() && get_post_type($id) == 'product') : // runs only on the shop page
$the_title = '<a href="' . get_the_permalink() . '">' . $the_title . '</a>';
endif;
return $the_title;
}