WordPress.org

Ready to get started?Download WordPress

Forums

Recent Posts - How to trim title length (2 posts)

  1. acialk
    Member
    Posted 5 years ago #

    Hey guys,

    I'm using a dynamic sidebar with several widgets and I would like to trim the title/links display length once they reach a certain number of characters. I've found a function that'll probably do what I need but I've got no idea how to apply it properly.

    /**
    * Add this to your page:
    * <?php
    * include "shorten_a_text_string.php";
    * echo ShortenText($text);
    * ?>
    * where $text is the text you want to shorten.
    *
    * Example
    * Test it using this in a PHP page:
    * <?php
    * include "shortentext.php";
    * $text = "The rain in Spain falls mainly on the plain.";
    * echo ShortenText($text);
    * ?>
    */

    <?php
    function ShortenText($text)
        {
            // Change to the number of characters you want to display
            $chars = 25;
            $text = $text." ";
            $text = substr($text,0,$chars);
            $text = substr($text,0,strrpos($text,' '));
            $text = $text."...";
            return $text;
        }
    ?>

    Hope you understand what I mean. Any help would be awesome, thanks.

  2. smoczokwik
    Member
    Posted 5 years ago #

Topic Closed

This topic has been closed to new replies.

About this Topic