Limit characters in description and show a link to read more

Hi! I would like to write a hook in function.php which allows me to display up to a limit of characters in the listing page. And if it’s longer than threshhold, I would like to redirect to a URL.

I am testing with this code below but it’s not working. Am I doing something wrong?

My guess is in the ‘hivepress/v1/forms/…’ part.

Thank you for your attention.

//Limit Characters

add _filter (
'hivepress/v1/forms/listing_description',
'hivepress/v1/forms/listing_CustomURL',  //This is a custom attribute
function ($description, $CustomURL) {
    // Check if the description has more than 250 characters
    if (strlen($description) > 250) {
        // Trim the description to 250 characters
        $trimmed_description = substr($description, 0, 250);
        
        // Append ellipsis (...) to indicate that the description was truncated
        $trimmed_description .= '...';
        $trimmed_description .= ' <a href="' . esc_url($CustomURL) . '">Read more</a>';

        
        return $trimmed_description;
    } else {
        // Return the original description if it's 250 characters or less
        return $description;
    },
1000
}

Unfortunately it will not work this way, there’s no such hook. The best way is overriding the listing/view/page/listing-description.php template part via a child theme, then you can customize it’s HTML accordingly (this template part shows the description on the listing page).

Hi Ihor, thank you so much for your suggestion.

I cannot find the php file in the path you described, will you please check the image and tell me if I followed correctly?

Thank you in advance.

I tried finding the parent file first, before creating the file to override in the child theme.

Also I tried searching for the string to find the file. I had no luck.

I need to know the file path to be able to override.

Hi,

Please check this link https://github.com/hivepress/hivepress/blob/master/templates/listing/view/page/listing-description.php

​I hope this is helpful to you.

oh wow andrii thanks.

It worked perfectly for my purposes.