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

  1. superkaratemonkeydeathcar
    Posted 3 years 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?



  2. superkaratemonkeydeathcar
    Posted 3 years 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:
    $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
    Posted 3 years 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 )

    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.


Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Featured Posts Grid
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic