Maze Posted November 13, 2016 Share Posted November 13, 2016 this is a RSS-question: Im pulling an RSS feed from a site to display on my homepage. It displays the titles correctly, but I wantto truncate the number of characters per post so it doenst take up a long section if the title is long. How do I do that? here is the code Im using <?php // Get RSS Feed(s) include_once(ABSPATH . WPINC . '/feed.php'); $rss = fetch_feed('http://examplesite.com/rss'); if (!is_wp_error( $rss ) ) : $maxitems = $rss->get_item_quantity(5); $rss_items = $rss->get_items(0, $maxitems); endif; ?> <ul> <?php if ($maxitems == 0) echo '<li>No items.</li>'; else foreach ( $rss_items as $item ) : ?> <li> <a href='<?php echo $item->get_permalink(); ?>' title='<?php echo 'Posted '.$item->get_date('j F Y | g:i a'); ?>'> <?php echo $item->get_title(); ?></a> </li> <?php endforeach; ?> </ul> well I am trying to limit the number of words displayed in the excerpt Basicly, the problem is that I want to use thesame excerpt in two different loops. The first one will display the whole excerpt, the second loop will only display a smallerpart of it. Therefore I cannot limit the words number of ALL the excerpts, but I would need to do that locally.Ideally, if there is a solution to this I can use the same excerpts in many different places onthe blog, using the same excerpt but in its longer/shorter version depending on the situation. <?php $little_excerpt = substr(the_excerpt(),0,XY); ?> doesn’t work because the_excerpt() is not at string.well we can Use get_the_excerpt() instead, that will do the trick. However, this is still limiting characters, not words.I just found a solution to limiting the number of words in the excerpt without plugins. Put the following piece of code in your templates functions.php file: <?php function string_limit_words($string, $word_limit) { $words = explode(' ', $string, ($word_limit + 1)); if(count($words) > $word_limit) array_pop($words); return implode(' ', $words); } ?> Next, put the following piece of code in your template where you want to display the excerpt: <?php $excerpt = get_the_excerpt(); echo string_limit_words($excerpt,25); ?> Where 25 is the number of words to display.php we can use PHP string functions like substr to return a number of characters rather than word count.The post content aaaaaaaaaaaaaaa aaaaaaaaaaaaa will be much longer than a a. $max_length = 150; echo substr( $string, 0, $max_length ); As a function it might look like: function string_limit_words( $string = '', $count = 25, $after = '...' ) { if ( strlen( $string ) <= $count ) { return $string; } return substr( $string, 0, $count ) . $after; } echo string_limit_words( "This is a lot of words", 10 ); // This is a ... the_excerpt() prints the output while get_the_excerpt() will return the string to allow manipulation before output. what do you say? look forward to hear from you Quote Link to comment https://forums.phpfreaks.com/topic/302529-truncate-a-rss-feed-with-php-string-functions-like-substr/ Share on other sites More sharing options...
Psycho Posted November 13, 2016 Share Posted November 13, 2016 (edited) I'm not going to read through your entire post. But, based on what I understand, I would create a function to output the limited string. Then when you need to output the title either use the title directly (if you want to use the whole thing), or use a function to limit the output accordingly. I have an old function for just this purpose. e.g. //Return a string up to a certain number of characters //but will break on a space function truncateString($string, $length, $ellipse='…') { //Return truncated string w/ ellipse (if >$length) return substr($string, 0, stripos($string, ' ', $length)) . $ellipse; } $string = "This is a string with a lot of words that may need to be limited in some places in the output"; echo "<b>Output the full string:</b><br>\n"; echo $string . "<br><br>\n"; echo "<b>Output the limited string:</b><br>"; echo truncateString($string, 25); Output: Output the full string:This is a string with a lot of words that may need to be limited in some places in the outputOutput the limited string: This is a string with a lot… Edited November 13, 2016 by Psycho 1 Quote Link to comment https://forums.phpfreaks.com/topic/302529-truncate-a-rss-feed-with-php-string-functions-like-substr/#findComment-1539290 Share on other sites More sharing options...
Maze Posted November 13, 2016 Author Share Posted November 13, 2016 hello dear Psycho, many many thanks for the quick reply. great to hear form you! fantastic to read your ideas - that sound great. i will add your lines accordingly ... .to the part that fetches the RSS <?php // Get RSS Feed(s) include_once(ABSPATH . WPINC . '/feed.php'); $rss = fetch_feed('http://examplesite.com/rss'); if (!is_wp_error( $rss ) ) : $maxitems = $rss->get_item_quantity(5); $rss_items = $rss->get_items(0, $maxitems); endif; ?> <ul> <?php if ($maxitems == 0) echo '<li>No items.</li>'; else and then your lines come....: . //Return a string up to a certain number of characters //but will break on a space function truncateString($string, $length, $ellipse='…') { //Return truncated string w/ ellipse (if >$length) return substr($string, 0, stripos($string, ' ', $length)) . $ellipse; } $string = "This is a string with a lot of words that may need to be limited in some places in the output"; echo "<b>Output the full string:</b><br>\n"; echo $string . "<br><br>\n"; echo "<b>Output the limited string:</b><br>"; echo truncateString($string, 25); Output: many many thanks for the hints. regards maze Quote Link to comment https://forums.phpfreaks.com/topic/302529-truncate-a-rss-feed-with-php-string-functions-like-substr/#findComment-1539292 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.