Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

php only show first few words of fetched post

Featured Replies

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?

 

:)

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

  • Author

Ok, but how do i put the two codes together so that i can make it work?

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.

  • 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?

  • 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!!!!!!!!! :D

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.