May 30, 201016 yr hi there! I have been trying to figure this out, and can't find a clear answer on the internet. I have this string where i am fetching post information. <p><?php query_posts('showposts=3'); ?> <?php while (have_posts()): the_post(); ?> <p ><?php the_title(); ?></p> <?php the_excerpt(); ?> <a href="<?php the_permalink(); ?>"> <img src="/images/arrow_sml.png" border="0" alt="go!"/></a><br /> <?php endwhile; ?></p> What i want to do is fetch only a few words of the the_excerpt, say about 20. So, i tried to use this code by putting it in the above coding; but it is not working. if(strlen($the_excerpt) > 20 ) { $totalChars = 20; $the_excerpt = substr($the_excerpt,0,$totalChars)."..."; } Does anyone know how to do this? Is it possible?
May 30, 201016 yr Should this part: if(strlen($the_excerpt) > 20 ) Not read: if(strlen($the_excerpt) >= 20 ) As that's the code for greater than or equal to (I think) And the line: $totalChars = 20; Is only asking for the first 20 characters of the post... Regards, Junior
May 30, 201016 yr Add this code to your theme's functions.php file: function new_excerpt_length($length) { return 20; } add_filter('excerpt_length', 'new_excerpt_length'); Then just use <?php the_excerpt(); ?> as you already have done.
May 30, 201016 yr Author Add this code to your theme's functions.php file: function new_excerpt_length($length) { return 20; } add_filter('excerpt_length', 'new_excerpt_length'); Then just use <?php the_excerpt(); ?> as you already have done. Hi Andyl, the good news is that the code is working, the bad news is that it is returning this error above my page - Warning: Cannot modify header information - headers already sent by (output started at /home/drumshac/public_html/wp-content/themes/drumshack/functions.php:73) in /home/drumshac/public_html/wp-content/plugins/wp-forum/wpf.class.php on line 1375! Any ideas?
May 30, 201016 yr Author Hi Andyl, the good news is that the code is working, the bad news is that it is returning this error above my page - Warning: Cannot modify header information - headers already sent by (output started at /home/drumshac/public_html/wp-content/themes/drumshack/functions.php:73) in /home/drumshac/public_html/wp-content/plugins/wp-forum/wpf.class.php on line 1375! Any ideas? oh, wait! It worked, sorry, I'd forgotten to change the theme page after editing the functions page! Thank you!!!!!!!!!
May 30, 201016 yr Hah, no worries PS. Check out the Wordpress Codex if you're stuck with things like this.
Create an account or sign in to comment