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


/ Published in: PHP
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  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());

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.