Adding a gap to the product name on store pages for a specific length

I am trying to get more consistent mesh in my store loop.

The product header covers more than 1 or 2 lines depending on the length of the line, so if the line length is less than the amount that makes it intersect with the next line, I want to add a gap "
" so that it does not affect the overall page interval of the store /

This is the code I tried at the moment:

<?php
    echo "test";
    $title = get_the_title();
    if ( strlen($title) < 29 )
    {
        echo '<br>';
    }
?>

I placed it in the woocommerce content-product.php template , but it does not work .

Is my code correct?

Any help would be greatly appreciated.

thank

+4
3

" " " ", .

woocommerce_template_loop_product_title() WooCommerce, content-product.php WooCommerce, .

, " , :

if (  ! function_exists( 'woocommerce_template_loop_product_title' ) ) {

    // Show the product title in the product loop. By default this is an <h3> html tag.

    function woocommerce_template_loop_product_title() {

        // Define the lenght limit for title (by line)
        $limit = 29;

        $title = get_the_title();
        $lenght = strlen($title);

        // 1. The title length is higher than limit

        if ( $lenght >= $limit ) {

            $title_arr1 = array();
            $title_arr2 = array();
            $sum_length_words = -1;

            // an array of the words of the title
            $title_word_arr = explode( ' ', $title );

            // iterate each word in the title
            foreach( $title_word_arr as $word ){
                // Length of current word (+1 space)
                $length_word = strlen($word) + 1;
                // Adding the current word lenght to total words lenght
                $sum_length_words += $length_word;
                // Separating title in 2 arrays of words depending on lenght limit
                if ( $sum_length_words <= $limit )
                    $title_arr1[] .= $word;
                else
                    $title_arr2[] .= $word;
            }
            // Converting each array in a string
            $splitted_title = implode(" ", $title_arr1). ' ('. strlen(implode(" ", $title_arr1)) .')';
            $splitted_title .= '<br>'; // adding <br> between the 2 string
            $splitted_title .= implode(" ", $title_arr2). ' ('. strlen(implode(" ", $title_arr2)) .')';
            echo '<h3>' . $splitted_title . '</h3>';

        // 2. The title length is NOT higher than limit

        } else {
            echo '<h3>' . $title . '</h3>';
        }

    }

}

function.php ( ), .

.

+1

, . , , , , , "Test Bank : , 5/E" "Test Bank for Medical Terminology..." .

,

0

h1{ height: 40px; width: 250px; white-space: nowrap;  overflow: hidden;   text-overflow: ellipsis;}
<h1>Sample test test test test</h1>
Hide result

, . , , ,

0
source

Source: https://habr.com/ru/post/1652982/


All Articles