Posted By

elightbo on 01/20/10


Tagged

textmate wordpress title


Versions (?)

Previou and next post links wth ability to shorten titles


 / Published in: PHP
 

URL: From WordPress plugin: http://wordpress.org/extend/plugins/better-nearby-posts-links/

  1. function my_prev_post_link($format='« %link', $link='%title', $in_same_cat = false, $excluded_categories = '', $string_lenght=22, $pre_link) {
  2.  
  3. if ( is_attachment() )
  4. $post = & get_post($GLOBALS['post']->post_parent);
  5. else
  6. $post = get_adjacent_post($in_same_cat, $excluded_categories, true);
  7.  
  8. if ( !$post )
  9. return;
  10.  
  11. $title = apply_filters('the_title', $post->post_title, $post);
  12.  
  13. //create substring of the title to the last space and add dots
  14. if (strlen($post->post_title) >= ($string_lenght+1)){
  15. $short = substr($title,0,$string_lenght);
  16. if (substr_count($short," ") > 1) {
  17. $lastspace = strrpos($short," ");
  18. $short = substr($short,0,$lastspace);
  19. }
  20. $dots = '...';
  21. }
  22. else {
  23. $short = $post->post_title;
  24. $dots = '';
  25. }
  26. $string = '<a href="'.get_permalink($post->ID).'" title="previous post: '.$title.'">';
  27. $link = str_replace('%title', $short.$dots, $link);
  28. $link = $pre . $string . $link . '</a>';
  29. $format = str_replace('%link', $link, $format);
  30. echo $pre_link.$format;
  31. }
  32.  
  33. function my_next_post_link($format='%link &raquo;', $link='%title', $in_same_cat = false, $excluded_categories = '', $string_lenght=22, $pre_link) {
  34.  
  35. if ( is_attachment() )
  36. $post = & get_post($GLOBALS['post']->post_parent);
  37. else
  38. $post = get_adjacent_post($in_same_cat, $excluded_categories, false);
  39.  
  40. if ( !$post )
  41. return;
  42.  
  43. $title = apply_filters('the_title', $post->post_title, $post);
  44.  
  45. //create substring of the title to the last space and add dots
  46. if (strlen($post->post_title) >= ($string_lenght+1)){
  47. $short = substr($title,0,$string_lenght);
  48. if (substr_count($short," ") > 1) {
  49. $lastspace = strrpos($short," ");
  50. $short = substr($short,0,$lastspace);
  51. }
  52. $dots = '...';
  53. }
  54. else {
  55. $short = $post->post_title;
  56. $dots = '';
  57. }
  58. $string = '<a href="'.get_permalink($post->ID).'" title="next post: '.$title.'">';
  59. $link = str_replace('%title', $short.$dots, $link);
  60. $link = $pre . $string . $link . '</a>';
  61. $format = str_replace('%link', $link, $format);
  62. echo $pre_link.$format;
  63. }

Report this snippet  

You need to login to post a comment.