tags:

views:

41

answers:

6

I have a certain amount of characters that I can fit in the spot I want to display the category. So I was wondering if there was a way I could limit the ammount of characters that this function can output.

I want to limit this functions output:

<?php the_category(', ') ?>
outputs: WordPress, Computers, Blogging

Something like this:

<?php char_limit(the_category(', '), 14, '...') ?>
outputs: WordPress, Com...

I'm new to php and wordpress, so a litle guidance on how to execute would help.

+4  A: 

webdestroya's comment and a rereading of the question leads me to suggest substr_replace():

echo substr_replace(the_category(', '),"...",14);

I had originally suggested word_wrap(), which doesn't truncate.

Vinko Vrsalovic
This isn't exactly what he is looking for... he doesn't want to wrap the string, he wants to truncate it.
webdestroya
@webdestroya: Correct, I had misread the question, fixed.
Vinko Vrsalovic
Is there a simple way to change the wordpress function the_category(', ') to a string? Because its not working on any functions I pass though.
ThomasReggi
+2  A: 

See the substr function.

Artefacto
+1  A: 

You could use a substring to create a truncate function

function truncate ($str, $length=10, $trailing='...')
{
/*
** $str -String to truncate
** $length - length to truncate
** $trailing - the trailing character, default: "..."
*/
      // take off chars for the trailing
      $length-=mb_strlen($trailing);
      if (mb_strlen($str)> $length)
      {
         // string exceeded length, truncate and add trailing dots
         return mb_substr($str,0,$length).$trailing;
      }
      else
      {
         // string was already short enough, return the string
         $res = $str;
      }
 
      return $res;
 
}

got it from here

Nealv
A: 

this will get you it, with ellipsis

function shorten ($str, $len)
{
    if (strlen($str) > $len) 
    {
        return substr($str, 0, $len) . "...";
    else
    {
        return $str;
    }
}
babonk
A: 

You may want to use an excerpt of your post - there is a field that you can fill and show with something more meaningful than just the first x characters

here is a link that explains what excerpt is : http://codex.wordpress.org/Excerpt

here is the function reference http://codex.wordpress.org/Function_Reference/the_excerpt

Basically you can use : <?php the_excerpt(); ?>

or in your case :

<?php if ( is_category() || is_archive() ) {
    the_excerpt();
} else {
    the_content();
} ?>

you can control the excerpt length using this :

function new_excerpt_length($length) {
    return 20;
}
add_filter('excerpt_length', 'new_excerpt_length');

If you don't want to use excerpt idea , you can still go the old way :

<?php
$string =  get_the_content('');
$newString = substr($string, 0, 20);
echo $newString;
?>

Hope that helped :)

You may also wanna take a look at plugins

mireille raad
My question was refering to the_category(). It just displays a list of the categories specific to the post. Not referring to the content area.
ThomasReggi
A: 

If you look at what the_category is, its simply a wrapper function:

function the_category( $separator = '', $parents='', $post_id = false ) {
    echo get_the_category_list( $separator, $parents, $post_id );
}

Just use something like this:

$catList = get_the_category_list(', ');
if(strlen($catList)>14)
     $catList = substr($catList,0,14) . "&#8230;";
bhamrick