Limit the Post Title in WordPress

Sometime we have very long post title and we want that only some part of title should display on main page and whole title should only display on single post page. Following code you can use to perform this task.
<?php
            foreach( $myposts as $post ):setup_postdata($post)

                          $post_title=get_the_title();
  $title_length=strlen($post_title);

  $title_sub=substr($post_title,0,29);
if($title_length<30)
the_title();
else
 echo $title_sub.”…”;
?>
If you will find any problem just mention it in comments, I will try to resolve it.


Posted

in

,

by

Tags: