Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter superkaratemonkeydeathcar

    (@superkaratemonkeydeathcar)

    I went out and found user Alchemyth’s help in this thread: http://wordpress.org/support/topic/limit-excerpt-length?replies=11

    But I don’t really know enough PHP to make it work properly. I know I’m doing something wrong, but am having trouble figuring it out.

    So far, from Line 403 of featured-posts-grid.php, I have:
    <code>
    $post_details[$key][‘post_title’] = $text;
    $text = $val->post_title;
    if (strlen($text) > 50) {
    $text = substr($text,0,strpos($text,’ ‘,50)); }
    else $text = $val->post_title;
    $text = $text . ‘ …’;
    `

    -But this doesn’t work.
    It skips the first of my 5 thumbnails across (only 1 row), and then starts putting the titles of entries 1-4 into images 2-5.

    Any code help would be appreciated.

    (that bit up there is Alchemyth’s trick to limit the length, chop off the last broken word and add an ellipsis, ie: …)

    Thanks for any help!

    Thread Starter superkaratemonkeydeathcar

    (@superkaratemonkeydeathcar)

    Ok, for those who want to give this a shot, I figured it out with my non-existant PHP skills.

    Ok, so back near 399 of featured-posts-grid.php, after

    global $post;
    $temp_post = $post;

    I just made a function, where I could pass the $val->post_title and a limit as arguments.

    So, it goes:

    function limit_chars ($string, $limit) {
    			if (strlen($string) > $limit) {
    				$text = substr($string,0,strpos($string,' ',$limit)); }
    			else $text = $string;
    			$text = $text . ' ...';
    			return ($text);
    }

    Then in the next block, where it pulls down all the elements of each post it throws, right after the call:

    foreach ( $recent_posts as $key=>$val )
        {
            setup_postdata($val);

    I pull up my little character+word-break+… limiter with this:
    $post_details[$key]['post_title'] = limit_chars($val->post_title, 45);

    -And then let the blocks for excerpt, author, etc. run normally.

    Took awhile, but I just tested it now and it seems to run great! ::knock on wood::

    So, there you are. If anyone else needs it, it appears to work.

    Cheers!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Limit Title To x Words / Characters?’ is closed to new replies.