Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trim characters from content instead of words - Wordpress

Tags:

php

wordpress

So I've been looking for a solution for quite some time. But can't find it somehow.

What I need is a function that shows a specific amount of characters from the content, instead of a specific number of words. Because words can be longer than other words and I want to keep the styling from the post preview equal.

Now I'm still using trim words:

<p><?php echo wp_trim_words( get_the_content(), 15 ); ?></p>

Does anyone know how I can trim characters from the content of a post instead of the amount of words?

Thank you in advance!

Update:

Here is my complete posts section:

    <?php
      $args = array(
        'post_type' => 'post',
        'posts_per_page' => 3,
        'category__in' => array(2, 3),
        'post__not_in' => array( $post->ID ),
      );
    ?>
    <?php $query = new WP_Query($args); ?>
    <?php if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?>

      <a href="<?php the_permalink();?>">
        <div class="post">
          <?php $thumb = get_the_post_thumbnail_url(); ?>
          <div class="post-image" style="background-image:url('<?php echo $thumb;?>');"></div>
          <div class="post-prev">
            <?php
            foreach (get_the_category() as $category){
              echo "<span>";
              echo $category->name;
              echo "</span>";
            } ?>
            <h2>
              <?php
                $thetitle = $post->post_title;
                $getlength = strlen($thetitle);
                $thelength = 20;
                echo substr($thetitle, 0, $thelength);
                if ($getlength > $thelength) echo "..";
              ?>
            </h2>
            <p><?php echo wp_trim_words( get_the_content(), 15 ); ?></p>
            <span class="btn">Lees verder</span>
          </div>
        </div>
      </a>

    <?php endwhile; wp_reset_postdata(); else : ?>
      <p><?php _e("Geen content gevonden.."); ?></p>
    <?php endif; ?>

2 Answers

To avoid cutting words, I am using the following custom function:

function theme_truncate( $string, $length = 100, $append = '&hellip;' ) {
$string = trim( $string );

if ( strlen( $string ) > $length ) {
    $string = wordwrap( $string, $length );
    $string = explode( "\n", $string, 2 );
    $string = $string[0] . $append;
}

return $string;}

It is using PHP wordwrap and explode to achieve the goal.

You can, later on, call this function like this:

echo esc_html( theme_truncate( get_the_content(), 15 ) );
like image 77
Saqib Avatar answered Sep 02 '25 07:09

Saqib


If you want exactly 15 characters without any HTML in the string, you can do it in steps:

First get the string and remove the HTML using strip_tags():

$content = strip_tags(get_the_content());

Then you grab the 15 first characters from the now HTML-less string using substr():

echo substr($content, 0, 15);

That would do it.

You can also do it as a one-liner:

echo substr(strip_tags(get_the_content()), 0, 15);
like image 36
M. Eriksson Avatar answered Sep 02 '25 06:09

M. Eriksson