How do I truncate this PHP
- Started
- Last post
- 16 Responses
- effour
Hi,
How do I truncate this to 20 words?
<?php the_content(); ?>Any help would be appreciated.
Thanks,
Ryan
- lukus_W0
use the power of regular expressions
- effour0
enlighten me please, i tried and failed, i need guidance
- Shaney0
if it's wordpress use
the_excerpt()
instead of
the_content()
and set the excerpt length in the admin panel
- lukus_W0
post the code describing how far you've got so far
- effour0
it's a custom 'featured items' .php page so the excerpt function doesn't work on it, here's the code:
<div id="item1" class="spotlight-item">
<img src="<?php echo get_post_meta($post->ID, 'preview_image',true) ?>" alt="Featured Project" class="preview png_bg" />
<div class="description">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<a href="<?php echo get_post_meta($post->ID, 'project_link',true) ?>"><span class="launch png_bg">Launch Project</span></a>
</div><!-- end description --></div>
- acescence0
check out get_the_content()
returns content without formatting.
- lukus_W0
I just used google ..
- acescence0
the problem is that if you just truncate the results of the_content, you'll have orphaned tags. you have to truncate it first, then apply formatting.
- acescence0
here you go...
the number 100 in the second line is the number of chars you want in the excerpt. you could also break it up by word instead.
- effour0
This worked great, anyone know how I can make sure it doesn't cut off words? It currently cuts off at characters
Thanks!
$mycontent = get_the_content();
$mycontent = substr($mycontent, 0, 100)."...";
$mycontent = apply_filters('the_content', $mycontent);
$mycontent = str_replace(']]>', ']]>', $mycontent);
echo $mycontent;
- Mick0
Rather than just using substring, you could split the text block using the space character so it splits all words and converts them to an array, then trim the array to how many words you want, then joing the array back to a string. This would give you a word limit, but if you need the text to fit somewhere it won't be much good if people enter exceptionally long or short words since there will be a big variation in how much space the string will take up.
- acescence0
the number 20 in there is the number of words..
- effour0
You are a god acescence!
- raf0
I just installed Page Excerpt plugin. Yes you guessed it: it enables excerpts for pages which you can access via the_excerpt()
- OSFA0
So, acescense, since you are in a giving mood...
would you be kind enough to show me or send me the PHP for a fuckin contact form that is fuckin driving me fuckin crazy?!?