WordPress.org

Ready to get started?Download WordPress

Forums

Featured Posts Grid
[resolved] Limit Title To x Words / Characters? (3 posts)

  1. superkaratemonkeydeathcar
    Member
    Posted 1 year ago #

    I tend to write long titles.

    These would overwhelm the size of the thumbs I'm trying.

    Any code hack to limit the length of titles?

    Thanks!

    http://wordpress.org/extend/plugins/featured-posts-grid/

  2. superkaratemonkeydeathcar
    Member
    Posted 1 year ago #

    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!

  3. superkaratemonkeydeathcar
    Member
    Posted 1 year ago #

    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!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic