My products have titles that include the delimiter |
after which I enter some SEO keywords.
Example product title Samsung UE55AU7172 | Smart 4K UHD 55
Can I force WooCommerce to show the product titles up to that delimiter in product category pages only?
For example, the above product title in the product category page would be Samsung UE55AU7172
.
Advertisement
Answer
You can use the woocommerce_shop_loop_item_title
action hook to edit the title on a product category archive page.
So you get:
/** * 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 );
OR use this to apply it on WooCommerce archive / shop / cat pages.
/** * 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 );