Woocommerce覆盖分组产品标题

时间:2013-08-12 03:09:49

标签: woocommerce

我在其他主题上看过这个,但无法弄清楚它是如何完成的。在我的分组产品列表中,标题列为父产品标题 - >儿童产品标题。我只需要列出Child产品标题。

我可以看到要改变的代码是:

<a href="' . get_permalink( $child_product['product']->id ) . '">' . $child_product['product']->post->post_title . '</a>

post_title是否需要覆盖,或者代码改为......什么?

2 个答案:

答案 0 :(得分:3)

(虽然这已经很老了,但仍然是谷歌排名显着的常见问题)

Woocommerce定义了一个过滤器woocommerce_product_title,它允许您通过修改其显示方式的函数传递产品的标题。

添加过滤器,可能在您的主题functions.php

add_filter('woocommerce_product_title', 'clean_up_title');

这是我目前用来实现这一目标的功能,没有任何承诺,这是最好的方式:

function clean_up_title($title){
    // check to see if there is an html arrow in the title
    if (strpos($title, '&rarr;')){
        $separator = '&rarr;';
    }

    // otherwise assume it's the character 
    else {
        $separator = '→';
    }

    // split the title into multiple parts at the arrows
    $prog_array = explode($separator, $title);

    // get the last part, the actual product name
    $prog_name = end($prog_array);

    // slice off any leading or trailing whitespace
    return trim($prog_name);
}

答案 1 :(得分:3)

这样做会更清洁。只需“返回”产品标题而不是“编辑”它。

function clean_product_title($title, $product) {

   return $product->post->post_title;;

}

add_filter('woocommerce_product_title', 'clean_product_title', 10, 2);
相关问题