• Hello,

    Using Arras 1431 and WP3, I have used the following code in tapestries.php in a post summary on the main page.

    $postheader .= ‘<abbr class=”published” title=”‘ . get_the_title() . ‘”>’ . get_the_title() . ‘</abbr></span>’;

    Does anyone know how I could amend this code, so it displays only the first two words of the title?

    this is the corresponding css

    .posts-default .published { text-transform: uppercase; float: left; margin: 5px 5px 5px; width:111px; font-size: 10px; color:#CC9; border-bottom: none; overflow: hidden;}

Viewing 10 replies - 1 through 10 (of 10 total)
  • $postheader .= '<abbr class="published" title="' . get_the_title() . '">' . substr(get_the_title(), 0 , 2) . '</abbr></span>';

    Try this.

    Thread Starter yesmaybe

    (@yesmaybe)

    Hi Chinmoy,

    That’s great thanks.

    However, it doesn’t display the – correctly, it came up as a �.

    I take it the 2 is the letter count, but can you explain to me what that code does, the 0,2 so I have a better understanding how to manipulate it?

    Many thanks,
    Andy

    substr
    (PHP 3, PHP 4 )

    substr — Return part of a string
    Description
    string substr ( string string, int start [, int length])

    substr() returns the portion of string specified by the start and length parameters.

    If start is non-negative, the returned string will start at the start’th position in string, counting from zero. For instance, in the string ‘abcdef’, the character at position 0 is ‘a’, the character at position 2 is ‘c’, and so forth.

    Example:

    <?php
    $rest = substr("abcdef", 1);    // returns "bcdef"
    $rest = substr("abcdef", 1, 3); // returns "bcd"
    $rest = substr("abcdef", 0, 4); // returns "abcd"
    $rest = substr("abcdef", 0, 8); // returns "abcdef"
    
    // Accessing via curly braces is another option
    $string = 'abcdef';
    echo $string{0};                // returns a
    echo $string{3};                // returns d
    ?>

    Thread Starter yesmaybe

    (@yesmaybe)

    thanks very much for the explanation. it helps a lot.

    hi,
    I have used the following code in my theme functions.php to shorten post title on the home page:

    // WordPress Hack by Knowtebook.com
    // Shorten any text you want
    
    function ShortTitle($text)
    
    {
    
    // Change to the number of characters you want to display
    
    $chars_limit = 30;
    
    $chars_text = strlen($text);
    
    $text = $text." ";
    
    $text = substr($text,0,$chars_limit);
    
    $text = substr($text,0,strrpos($text,' '));
    
    // If the text has more characters that your limit,
    //add ... so the user knows the text is actually longer
    
    if ($chars_text > $chars_limit)
    
    {
    
    $text = $text."...";
    
    }
    
    return $text;
    
    }

    and the following line in place of the_title in the page template:

    <?php echo ShortTitle(get_the_title()); ?>

    and it works fine in my theme

    Great!!! Awesome work @nicovece. Please mark this topic as resolved.

    Thanks
    C

    np

    does anyone care that @yesmaybe as the owner of this thread asked to limit the title to a certain number of words – not characters?

    <?php $short_title = explode(' ',get_the_title()); $short_title = $short_title[0].' '.$short_title[1]; echo $short_title; ?>

    Thank you nicovece!

    I was looking for this function!! 😀

    Thank you very much nicovece!

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘limit length of displayed text get_the_title’ is closed to new replies.