Click here to Skip to main content
14,175,712 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi guys.

I am new to php and trying to truncate these lines of text ('information') in my custom WP theme:
<div class="overskrift"><?php echo get_post_meta(get_the_ID(),'information',true); ?></div>
Any suggestion on how to do it. I tried with the following code, but can't seem to get it to work:
{truncate_post( 130 );}
Basically what i want to do is to make sure all my blog posts have the same lenght on the archive page, as they are being shown in 3 columns. If one post's description ('information') text is longer than the other, the grid will not look good. I need all posts to have the same lenght on the blog/archive page.

If this function is used correctly, i have seen WP cuts off the text at a certain point (after 130 characters in my case) and adds a "..." at the end.

Can anybody tell me how to achieve that effect.


What I have tried:

{truncate_post( 130 );}
Updated 5-Mar-19 3:19am
ZurdoDev 5-Mar-19 8:59am
What do you mean by "truncate"? Truncate means to cut off the rest of some string. php probably has a LEFT method to take the LEFT most characters if that is what you want.
emilianoD85 5-Mar-19 9:12am
Sorry i wasn't clear enough.

I want to cut of texts/descriptions longer than 130 characters for each post in my blog/archive section. My blog page has 3 columns of posts, so if i don't cut them of at a certain point, some posts will be longer than other - i don't want that.

WP has this function already, as i have seen it before. When used correctly, it just adds a "..." at the end of your texts, cutting them off at the "XXX" mark - in this case i want that to be after 130 characters.

Do you follow me?
ZurdoDev 6-Mar-19 9:02am
There is no magic or built in way to do it. If you like what another site has done I would inspect their html to see how they did it. Look at the overflow property on a div.
Richard Deeming 5-Mar-19 10:16am
NB: Unless you're using a fixed-width font, different characters will be different widths. 130 characters from one post will not necessarily be the same size as 130 characters from another post.
emilianoD85 5-Mar-19 10:56am
I am. Same font size, family, letter-spacing and all.
emilianoD85 6-Mar-19 4:22am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web06 | 2.8.190525.1 | Last Updated 5 Mar 2019
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100