-2

I am new to php and trying to trim the lines of text generated by the ('information') part of the code seen below (in my custom WP theme):

<div class="information"><?php echo get_post_meta(get_the_ID(),'information',true); ?></div> 

Each post has a different lenght of it's information (basically like a excerpt, but pulled from a custom field in each post) text, which messes up my archive/blog page, as i use a 3 columns grid. I need the lenght of all posts seen on the archive page to be equally long, by restricting the "information" text to be no more than lets say 150 characters long.

I see the fenction in this piece of code for example, taken from a default WP file, but can't seem to wrap it around my own piece of code seen above, to make it work like i want it to:

<?php
et_divi_post_meta();

if ( 'on' !== et_get_option( 'divi_blog_style', 'false' ) || ( is_search() && ( 'on' === get_post_meta( get_the_ID(), '_et_pb_use_builder', true ) ) ) ) {
    truncate_post( 270 );
} else {
    the_content();
}
?>

This is what i want/talk about:

Before:

POST 1 I am a wordpress post, with a long text, that should not be so long, as it makes the 3 column setup of the archive page look stupid.

After:

POST 1 I am a wordpress post, with a long text, that should not be so long...

How do i do this?

Thanks!

emiliano85
  • 129
  • 10
  • `I am a wordpress post, with a long text, that should not be so long, as it makes the 3 column setup of the archive page look stupid.` is only 132 characters. Something like `mb_substr($content, 1, 150) . '...'` is probably your answer with `mb_strlen($content) > 150 ? ` – user3783243 Mar 05 '19 at 17:04
  • Just FYI, you probably had a hard time finding this answer on google because you were searching for "trim" but generally speaking people refer to this as "truncating" a string, not trimming, trimming is generally implied to remove whitespace from a string – Zachary Craig Mar 05 '19 at 17:04
  • Hi zack. Thanks. But i actually started out by using the word truncate, which made people ask what i meant, as that was notmally used to achieve other things in php. See one of the answers below, to give you an idea: "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." – emiliano85 Mar 05 '19 at 17:06

2 Answers2

0

You can use substr() php function to get part of the string.

Take a look at this example function.

function trimResult($string, $len = 150){

    if(strlen($string)> $len){
        $string = substr($string,0,$len) . "..."; // to let the user know that was truncated...
    }

    return $string;
}

ref: http://php.net/substr

Specific code example, will be a better solution to include the php function on a global file.

<?php
function trimResult($string, $len = 150){

    if(strlen($string)> $len){
        $string = substr($string,0,$len) . "..."; // to let the user know that was truncated...
    }

    return $string;
}
?>

<div class="information"><?php echo trimResult(get_post_meta(get_the_ID(),'information',true)); ?></div>

Hope it helps.

Vidal
  • 2,605
  • 2
  • 16
  • 32
0
$post = "I am a wordpress post, with a long text, that should not be so long, as it makes the 3 column setup of the archive page look stupid.";

$post = mb_strimwidth($post, 0, 70, "...");

echo $post;

result: "I am a wordpress post, with a long text, that should not be so long..."

Add ... if string is too long PHP

Andrew
  • 18,680
  • 13
  • 103
  • 118