Lego2012
12/13/2016 - 3:27 PM

Truncate Post Title

Truncate Post Title

<?php
//* Do NOT include the opening php tag shown above. Copy the code shown below.

function customTitle($limit) {
    $title = get_the_title($post->ID);
    if(strlen($title) > $limit) {
      $title = substr($title, 0, $limit) . '...';
    }
    echo $title;
}

<!-- Truncate to nearest whole word: -->

/** ====================================================================================

 * Shorten Post Title function by Sridhar Katakam 
 
==================================================================================== **/
function truncate_post_title( $limit ) {

    global $post;
    $post_title = get_the_title( $post->ID );
    
    if ( strlen( $post_title ) > $limit ) {
      $post_title = preg_replace('/\s+?(\S+)?$/', '...', mb_substr($post_title, 0, $limit));
    }

    return $post_title;
}