在WooCommerce类别存档页面上按特定分隔符显示产品标题



我的产品的标题中包含分隔符|,然后我输入一些SEO关键字。

示例产品标题Samsung UE55AU7172 | Smart 4K UHD 55

我可以强制WooCommerce仅在产品类别页面中显示该分隔符之前的产品标题吗?

例如,产品类别页面中的上述产品标题将为Samsung UE55AU7172

您可以使用woocommerce_shop_loop_item_title操作挂钩编辑产品类别归档页面上的标题。

所以你得到了:

/**
* Show the product title in the product loop.
*/
function action_woocommerce_shop_loop_item_title() {
// Returns true when viewing a product category archive
if ( is_product_category() ) {  
// Removes a function from a specified action hook.
remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10 );

// Get the title
$title = get_the_title();

// String contains a specific word
if ( strpos( $title, '|' ) !== false ) { 
// Remove portion of a string after a certain character
$title = substr( $title, 0, strpos( $title, '|' ) );
}

// Output
echo '<h2 class="' . esc_attr( apply_filters( 'woocommerce_product_loop_title_classes', 'woocommerce-loop-product__title' ) ) . '">' . $title . '</h2>';
}
}
add_action( 'woocommerce_shop_loop_item_title', 'action_woocommerce_shop_loop_item_title', 9 );

使用此功能将其应用于WooCommerce存档/商店/猫页面。

/**
* Show the product title in the product loop.
*/
function action_woocommerce_shop_loop_item_title() {
// Removes a function from a specified action hook.
remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10 );

// Get the title
$title = get_the_title();

// String contains a specific word
if ( strpos( $title, '|' ) !== false ) { 
// Remove portion of a string after a certain character
$title = substr( $title, 0, strpos( $title, '|' ) );
}

// Output
echo '<h2 class="' . esc_attr( apply_filters( 'woocommerce_product_loop_title_classes', 'woocommerce-loop-product__title' ) ) . '">' . $title . '</h2>';
}
add_action( 'woocommerce_shop_loop_item_title', 'action_woocommerce_shop_loop_item_title', 9 );

您可以使用以下代码来实现您想要的

remove_action( 'woocommerce_shop_loop_item_title','woocommerce_template_loop_product_title', 10 );
add_action('woocommerce_shop_loop_item_title', 'remove_strings_from_title', 10 );
function remove_strings_from_title() {

$title = get_the_title();
$title = strstr($title, '|', true);
if ($title == ""){$title = get_the_title();}


echo '<p class="' . esc_attr( apply_filters( 'woocommerce_product_loop_title_classes', 'woocommerce-loop-product__title' ) ) . '">' . $title . '</p>';
}

代码进入functions.php tested&工作

最新更新