While working for a client, we have faced a challenge in WordPress – Shorten the post titles in grid blocks or archive pages and display full title in single page or post area.
To work on this, we advice you to install Code Snippets Plugin and enable it. Also, take a backup of your WordPress using All-in-One WP Migration, for safety purpose.
WordPress – Shorten the post titles (Methods):
Method 1:
To shorten the title in blocks or archive pages, First you need place this below code in functions.php or via code snippets plugin.
function the_title_limit($length, $replacer = '...') { $string = the_title('','',FALSE); if(strlen($string) > $length) $string = (preg_match('/^(.*)\W.*$/', substr($string, 0, $length+1), $matches) ? $matches[1] : substr($string, 0, $length)) . $replacer; echo $string; }
After pasting this code, Save and active the code in Code Snippets. Then wherever you need your title to be short, Place the below code there and it will be shorten to the number mentioned in it.
<?php the_title_limit( 30, '...'); ?>
In our code, it is mentioned as 30. So the characters will be shorten or reduced to 30 characters. You can set your own number instead of 30 and “…” to read more or any text you wish to display.
Method 2:
Follow the same steps as Method 1, but replace the codes of Method 1 (If Method 1 doesn’t work)
function short_title($before = '', $after = '', $echo = true,$length = false) { $title = get_the_title(); if ( $length &amp;&amp; is_numeric($length) ) { $title = substr( $title, 0, $length ); } if ( strlen($title)> 0 ) { $title = apply_filters('short_title', $before . $title .$after, $before, $after); if ( $echo ) echo $title; else return $title; } }
<?php // short_title('BeforeText'. 'ellipsis', 'true', 'wordlength') short_title('**','...',true, '20'); ?>
Method 3: (Word Length)
Follow the same steps as Method 1, but replace the codes of Method 1 & 2 (If Method 1 & 2 doesn’t work)…
function short_title($after = '', $length) { $mytitle = explode(' ', get_the_title(), $length); if (count($mytitle)>=$length) { array_pop($mytitle); $mytitle = implode(" ",$mytitle). $after; } else { $mytitle = implode(" ",$mytitle); } return $mytitle; }
<?php // short_title('ellipsis', 'wordlength') echo short_title('...', 10); ?>
Here in this code, 10 is the number mentioned to display no. of words instead of characters. So, Please note it down and change the number as per your needs.
Method 4: (Direct integration)
Unlike other methods mentioned above, it is little unique and different way. Here in this, you don’t need to insert any extra codes in code snippets. You can directly just insert the code where you need to display shorten title.
<?php if (strlen(the_title('','',FALSE)) > 30) { //Character length $title_short = substr(the_title('','',FALSE), 0, 30); // Character length preg_match('/^(.*)\s/s', $title_short, $matches); if ($matches[1]) $title_short = $matches[1]; $title_short = $title_short.' ...'; // Ellipsis } else { $title_short = the_title('','',FALSE); } ?> <?php echo $title_short ?>
If you have suggestions on this topic, Please comment below. So, we can look into it.
If you have doubt or suggestions on other topics, please mention it in our contact form and we will work on them and post it separately sooner.