Strip producttitles after certain number of characters

Strtip producttitle after certain number of characters.

add_filter('the_title', 'sitiweb_shorten_woo_product_title', 10, 2);
function sitiweb_shorten_woo_product_title($title, $id)
{
  $link = get_the_permalink($id);
  $stip_after = 45; // strip after 45 characters.
  if (is_shop() && get_post_type($id) === 'product') {

    if (strlen($title) > $stip_after) {


      return '<a href="' . $link . '" class="link-custom-product">' . substr($title, 0, $stip_after) . ' &hellip;</a>'; // change last number to the number of characters you want
    }
    return '<a href="' . $link . '" class="link-custom-product">' . $title . '</a>';
  }
  return $title;
}

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest
Author picture

SitiWeb is de expert op het gebied van WordPress. gespecialiseerd in het leveren van maatwerk.

Leave a comment

Geef een reactie

Je e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *