Limit Title To x Words / Characters?
-
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!
-
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!
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!
- The topic ‘Limit Title To x Words / Characters?’ is closed to new replies.