Posted By

FlashBuddy on 10/13/13


Tagged

wordpressfunctionstextword-break


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

fhenderson


WordPress Function Split Name onto Two Lines (or any two strings/titles)


 / Published in: PHP
 

URL: http://flashalexander.com/split-name-wordpress-function/

I ran into a situation where I needed to break names into two lines. I tried css word-break: unsuccessfully, then came up with this function.

  1. // split name
  2.  
  3. if (!function_exists('the_title')) {
  4. function the_title($name) {
  5. if($name!='') { return get_the_title($name); }
  6. else return false;
  7. }
  8. }
  9.  
  10. if (!function_exists('split_name')) {
  11. function split_name($name) {
  12. if(strpos($name,' ')!==FALSE) {
  13. $name_arr = explode(' ',$name);
  14. if(count($name_arr)==2) { return $name_arr[0].'<br/>'.$name_arr[1]; }
  15. elseif(count($name_arr)==3) { return $name_arr[0].' '.$name_arr[1].'<br/>'.$name_arr[2]; }
  16. else { return $name; }
  17. } else { return $name; }
  18. }
  19. }
  20.  
  21. // Template usage - echo split_name(get_the_title());

Report this snippet  

You need to login to post a comment.