WordPress Snippets at WPcustoms

Limit post title character count

Truncate the post title and limit it to X characters. A quite easy function to set a max length to your post title. We used wpc_customtitle(50) which truncates the title to 50 characters ending with “..:”


/**
 * Snippet Name: Limit post title character count
 * Snippet URL: http://www.wpcustoms.net/snippets/limit-post-title-character-count/
 */
 // usage: wpc_customtitle(50)
function wpc_customTitle($limit) {
    $title = get_the_title($post->ID);
    if(strlen($title) > $limit) {
        $title = substr($title, 0, $limit) . '...';
    }
     
    echo $title;
}