Woocommerce:如何在类别页面中在标题上显示产品属性名称,并通过地址栏上的"?pa_attribute



所以我有一个产品属性(例如:颜色(,我只能在地址栏中使用?pa_color=red来显示类别中具有该属性的产品,因此如果我选择"*www.mysite.com/product-category/t-shirts/?pa_color=red*",我将只有以红色作为属性的T恤。这行得通。
问题是,当我进行"过滤"时,页面标题(产品列表上方(仍然只显示"T恤">,它没有指定它只显示红色T恤,这可能会使客户感到困惑。我需要的是让它显示类似"T恤(颜色:红色(">的东西。
我知道我可以使用侧边栏小部件来过滤属性,然后小部件将显示正在过滤的内容,但在这种特定情况下,这是一个非常小的设计,没有侧边栏或任何过滤,我需要它显示在标题中。

add_filter( 'woocommerce_page_title', 'custom_woocommerce_page_title', 15, 1 );
function custom_woocommerce_page_title( $page_title ) {
if ( is_archive() ) {
$exists_attr = false;
foreach ( $_GET as $index => $value ) {
if ( substr( $index, 0, 3 ) === 'pa_' ) {
$attr_id = wc_attribute_taxonomy_id_by_name( $index );
if ( $attr_id === 0 ) {
continue;
}
if ( ! $exists_attr ) {
$exists_attr = true;
$page_title .= ' (';
} else {
$page_title .= ', ';
}
$page_title .= wc_attribute_label( $index ) . ': ' . esc_html( $value );
}
}
if ( $exists_attr ) {
$page_title .= ')';
}
}
return $page_title;
}

这有效,但对 SEO 问题不利。 您的"页面"实际上并不存在,不会被谷歌编入索引。 真正的解决方案是创建一个真实的页面,例如: www.mysite.com/product-category/t-shirts/red

但这是类别和属性的混合,我不知道如何实现

最新更新