Truncating Post Titles
-
This is probably more of a php question than a WordPress question, but hopefully someone here can provide a little guidance.
There’s a bar below my navigation that displays the current page or post title. It shares space with the breadcrumb.
The problem with this, is that if the post title gets exceptionally long (as post titles sometimes do), this screws with the layout. So I’d like to truncate how the title displays if it’s over 30 characters.
I added the following code to my functions.php file:
//** Add ability to truncate titles function ShortenText($text) { // Function name ShortenText $chars_limit = 20; // Character length $chars_text = strlen($text); $text = $text." "; $text = substr($text,0,$chars_limit); $text = substr($text,0,strrpos($text,' ')); if ($chars_text > $chars_limit) { $text = $text."..."; } // Ellipsis return $text; }So normally, I’d just open the header.php file, and replace
<?php get_the_title(); ?>with this:
<?php echo ShortenText(get_the_title()); ?>Unfortunately, I’m using a ThemeWoot-based template, so few modifications are ‘simple’. But I managed to narrow down the source of the title to a single php file. The problem is that I can’t figure out the proper way to replace the code.
The current code reads:
/* * Page Title */ function page_title($type) { switch ($type) { case 'page': $title = get_the_title(); $desc = twoot_get_frontend_func('meta', 'page_desc'); $text_color = twoot_get_frontend_func('meta', 'page_desc_text_color'); $bg_color = twoot_get_frontend_func('meta', 'page_desc_bg_color'); $bg_image = twoot_get_frontend_func('meta', 'page_desc_bg_image'); $bg_repeat = twoot_get_frontend_func('meta', 'page_desc_bg_repeat'); $bg_horizontal = twoot_get_frontend_func('meta', 'page_desc_bg_horizontal'); $bg_vertical = twoot_get_frontend_func('meta', 'page_desc_bg_vertical'); break; case 'portfolio': $title = get_the_title(); $desc = twoot_get_frontend_func('meta', 'page_desc')==false? get_the_excerpt():twoot_get_frontend_func('meta', 'page_desc'); $text_color = twoot_get_frontend_func('meta', 'page_desc_text_color'); $bg_color = twoot_get_frontend_func('meta', 'page_desc_bg_color'); $bg_image = twoot_get_frontend_func('meta', 'page_desc_bg_image'); $bg_repeat = twoot_get_frontend_func('meta', 'page_desc_bg_repeat'); $bg_horizontal = twoot_get_frontend_func('meta', 'page_desc_bg_horizontal'); $bg_vertical = twoot_get_frontend_func('meta', 'page_desc_bg_vertical'); break; case 'archive': $desc = $text_color = $bg_color = $bg_image = $bg_repeat = $bg_horizontal = $bg_vertical = ''; if ( is_category() ) { $args = array ('<p>', '</p>'); $title = single_cat_title('',false); $desc = str_replace($args, '', term_description()); } elseif (is_day()) { $title = esc_attr__('Archive for date:','Twoot')." ".get_the_time('F jS, Y'); } elseif (is_month()) { $title = esc_attr__('Archive for month:','Twoot')." ".get_the_time('F, Y'); } elseif (is_year()) { $title = esc_attr__('Archive for year:','Twoot')." ".get_the_time('Y'); } elseif (is_search()) { switch (twoot_get_frontend_func('opt', 'opt', 'search_type')) { case '1': $types=array('post', 'page', 'portfolio'); break; case '2': $types=array('post', 'page'); break; case '3': $types=array('post'); break; case '4': $types=array('page'); break; case '5': $types=array('portfolio'); break; } // Get Query $query = new Twoot_Query(array('post_type' => $types)); $do_query = new WP_Query($query->do_global_query()); if(!empty($do_query->found_posts)) { if($do_query->found_posts > 1) { $title = $do_query->found_posts ." ". esc_attr__('search results for:','Twoot')." ".esc_attr__( get_search_query() ); } else { $title = $do_query->found_posts ." ". esc_attr__('search result for:','Twoot')." ".esc_attr__( get_search_query() ); } } else { if(!empty($_GET['s'])) { $title = esc_attr__('Search results for:','Twoot')." ".esc_attr__( get_search_query() ); } else { $title = esc_attr__('To search the site please enter a valid term','Twoot'); } } } elseif (is_author()) { $curauth = (get_query_var('author_name')) ? get_user_by('slug', get_query_var('author_name')) : get_userdata(get_query_var('author')); $title = esc_attr__('Author Archive','Twoot')." "; if(isset($curauth->nickname)) $title .= esc_attr__('for:','Twoot')." ".$curauth->nickname; } elseif (is_tag()) { $title = esc_attr__('Tag Archive for:','Twoot')." ".single_tag_title('',false); $desc = str_replace(array ('<p>', '</p>'), '', term_description()); } elseif(is_tax()) { $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); $title = $term->name; $desc = str_replace(array ('<p>', '</p>'), '', term_description()); } elseif (is_404()) { $title = esc_attr__('404 Page','Twoot'); } else { $title = esc_attr__('Archives','Twoot'); } if (isset($_GET['paged']) && !empty($_GET['paged'])) { $title .= " (".esc_attr__('Page','Twoot')." ".$_GET['paged'].")"; } break; } $html = '<header class="site-page-header">'; $html .= '<section class="entry-header">'; $html .= '<div class="container">'; $html .= '<div class="inner clearfix">'; $html .= '<h1 class="entry-title"><i class="icon icon-menu"></i>'.$title.'</h1>'; ob_start(); new twoot_breadcrumbs(); $html .= ob_get_clean(); $html .= '</div>'; $html .= '</div>'; $html .= '</section>'; if($desc) { $html .= '<section class="entry-desc"'.$this->page_title_style($text_color, $bg_color, $bg_image, $bg_repeat, $bg_horizontal, $bg_vertical).'>'; $html .= '<div class="container">'; $html .= '<div class="inner">'.stripslashes($desc).'</div>'; $html .= '</div>'; $html .= '</section>'; } $html .= '</header>'; return $html; }I know this is lengthy. I apologize. Unfortunately, I only know enough php to be dangerous. Any suggestions?
The topic ‘Truncating Post Titles’ is closed to new replies.