0

I'm trying to put a span class in the result of the title to be returned, however, I can not succeed to detect the class.

This is the original code:

//Limitar el número de caracteres en la página de tienda en WooCommerce
function short_woocommerce_product_titles_chars( $title, $id ) {
  // Para las paginas de categorías
  if ( is_home() || is_shop() || is_product_category() || is_product_tag() || 
    is_cart() && get_post_type( $id ) === 'product' ) { 
    // Si el título del producto es mayor a 30 caracteres
    if ( strlen( $title ) >= 35) { 
      // Acorta el título a 20 caracteres
      return substr( $title, 0, 34 ); 
    } else {
      // Pero si el título no es más largo de 30, pone el título completo
      return $title; 
    }
  } else {
    return $title;
  }
}
add_filter( 'the_title', 'short_woocommerce_product_titles_chars', 10, 2 );

I'm trying to put the following span class:

<span class="title-tooltip">' . $title . '</span>'

This is the final result code, without success of detecting the classe:

//Limitar el número de caracteres en la página de tienda en WooCommerce
function short_woocommerce_product_titles_chars( $title, $id ) {
  // Para las paginas de categorías
  if ( is_home() || is_shop() || is_product_category() || is_product_tag() || 
    is_cart() && get_post_type( $id ) === 'product' ) { 
    // Si el título del producto es mayor a 30 caracteres
    if ( strlen( $title ) >= 35) { 
      // Acorta el título a 20 caracteres
      return substr( '<span class="title-tooltip">' . $title . '</span>', 0, 34 ); 
    } else {
      // Acorta el título a 20 caracteres
      return $title;
    }
  } else {
    return $title;
  }
}
add_filter( 'the_title', 'short_woocommerce_product_titles_chars', 10, 2 );

This is the result in the front-end:

https://image.ibb.co/nJ09sn/span_class_title.jpg

As you can see in the image, the span class is shown in the frontend, it is deir, the classe is not detected.

I do not know what I'm doing wrong.

Some charitable soul that can help me? I have tried a thousand ways and nothing ... without success.

  • Would you please edit your question and provide the code that is actually outputting the title? Perhaps the output is being wrapped in an escape function... – Dave Romsey Mar 7 '18 at 2:53
0

You're substr() on the span which will chop the closing span off. I think what you want is:

return '<span class="title-tooltip">' . substr( $title, 0, 34 ) . '</span>'; 
  • The problem is that it does not detect me that it is an html class. I've put your code: image.ibb.co/fTm2dS/span_class_title3.png And in the front-end it goes like this: image.ibb.co/m1P5yS/span_class_title2.png – Nerea Mar 7 '18 at 0:40
  • 1
    It looks like another filter is running 'htmlspecialchars()` or similar on it after your code runs. Try a higher number priority on the filter. – Nathan Johnson Mar 7 '18 at 0:56
  • I have already tried it, however, it still does not execute the span class tag. – Nerea Mar 7 '18 at 2:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.