Skip to content
WJunction - Webmaster Forum

Post Title Shorter

Status
Not open for further replies.

4 comments

There are multiple ways of achieving this. One of them is do the following:

Place the following function in your functions.php file:

PHP:
function ShortenText($text) {  $chars_limit = 100;  $chars_text = strlen($text);  $text = $text." ";  $text = substr($text,0,$chars_limit);  $text = substr($text,0,strrpos($text,' '));
  if ($chars_text > $chars_limit)     { $text = $text."..."; }     return $text;}

You can change the variable
Code:
$chars_limit
to the number if characters you want to limit the title to.

Now add the following code into THE LOOP of wordpress mostly in your index.php:

PHP:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
 <h1><?php echo ShortenText(get_the_title()); ?></h1>
 <div class="post-title"><?php the_content(); ?></div>
<?php endwhile; endif; ?>
 
Status
Not open for further replies.

About the author

M
Active Member · Joined
95
Messages
12
Reactions
8
Points

Advertise on WJunction

Reach 1000's of webmasters, hosts & affiliates. Banner & sponsored-thread slots available.

Contact us
Back
Top Bottom