• Resolved handmadehome

    (@handmadehome)


    Hi,

    I’m using the code below to line break product titles, and it works fine on archive pages: https://www.shop.handmadehome.me/product-category/sculptures/

    But it doesn’t work in blocks: https://www.shop.handmadehome.me/

    Can you please help me to fix the code for blocks?

    Thank you!

    add_filter( 'the_title', 'custom_product_title', 10, 2 );
    function custom_product_title( $title, $post_id ){
        $post_type = get_post_field( 'post_type', $post_id, true ); 
    
        if( in_array( $post_type, array( 'product', 'product_variation') ) ) {
            $title = str_replace( '–', '<br/><br/>', $title ); // we replace "–" by "<br>"
        }
        return $title;
    }

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Hi @handmadehome

    Thanks for reaching out!

    The code you’re using is working fine on archive pages because it’s directly affecting the global post object, which is used by WordPress to display posts. However, blocks in WooCommerce are rendered differently and they don’t use the global post object, that’s why the code doesn’t work there.

    This is a bit of a complicated topic that would need some customization to address. Unfortunately, custom coding is not something we can assist with directly. However, I’ll keep this thread open for a bit to see if anyone from the community can lend a hand.

    If you have any other questions related to development or custom coding, don’t hesitate to reach out to some of the great resources we have available for support. The WooCommerce community is filled with talented open-source developers, and many of them are active on the channels listed below:

    Hope this helps!

Viewing 1 replies (of 1 total)
  • The topic ‘Title break code’ is closed to new replies.