OhTheNoes Posted June 3, 2010 Share Posted June 3, 2010 Hi. I'm working with a code I found online for parsing WordPress RSS. What I'd like to do is display something in this format: <a href="BlogEntry">Date of Blog Entry</a> What I have with the code now is exactly what I want, except it displayed the 10 latest entries. I only want to display the latest one. Without changing any WordPress settings, how can I limit it to display only the latest entry? Any help would be appreciated as I have very little PHP knowledge. Thank you! 1. <?php function parseRSS($url) { 2. $feedeed = implode('', file($url)); 3. $parser = xml_parser_create(); 4. xml_parse_into_struct($parser, $feedeed, $valueals, $index); 5. xml_parser_free($parser); 6. foreach($valueals as $keyey => $valueal){ 7. if($valueal['type'] != 'cdata') { 8. $item[$keyey] = $valueal; 9. } 10. } 11. $i = 0; 12. foreach($item as $key => $value){ 13. if($value['type'] == 'open') { 14. $i++; 15. $itemame[$i] = $value['tag']; 16. } elseif($value['type'] == 'close') { 17. $feed = $values[$i]; 18. $item = $itemame[$i]; 19. $i--; 20. if(count($values[$i])>1){ 21. $values[$i][$item][] = $feed; 22. } else { 23. $values[$i][$item] = $feed; 24. } 25. } else { 26. $values[$i][$value['tag']] = $value['value']; 27. } 28. } 29. return $values[0]; 30. } 31. $xml = parseRSS("http://www.domain.com/category/feed/"); 32. foreach($xml['RSS']['CHANNEL']['ITEM'] as $item) { 33. $fDate = mb_substr ($item['PUBDATE'],5,11); 34. echo("<p>Latest update: <a href=\"{$item['LINK']}\">"); 35. echo($fDate); 36. echo("</a></p>"); 37. } 38. ?> Quote Link to comment https://forums.phpfreaks.com/topic/203772-limiting-the-number-of-results-displayed/ Share on other sites More sharing options...
dabaR Posted June 3, 2010 Share Posted June 3, 2010 Try this: 1. <?php function parseRSS($url) { 2. $feedeed = implode('', file($url)); 3. $parser = xml_parser_create(); 4. xml_parse_into_struct($parser, $feedeed, $valueals, $index); 5. xml_parser_free($parser); 6. foreach($valueals as $keyey => $valueal){ 7. if($valueal['type'] != 'cdata') { 8. $item[$keyey] = $valueal; 9. } 10. } 11. $i = 0; 12. foreach($item as $key => $value){ 13. if($value['type'] == 'open') { 14. $i++; 15. $itemame[$i] = $value['tag']; 16. } elseif($value['type'] == 'close') { 17. $feed = $values[$i]; 18. $item = $itemame[$i]; 19. $i--; 20. if(count($values[$i])>1){ 21. $values[$i][$item][] = $feed; 22. } else { 23. $values[$i][$item] = $feed; 24. } 25. } else { 26. $values[$i][$value['tag']] = $value['value']; 27. } 28. } 29. return $values[0]; 30. } 31. $xml = parseRSS("http://www.domain.com/category/feed/"); 32. foreach($xml['RSS']['CHANNEL']['ITEM'] as $item) { 33. $fDate = mb_substr ($item['PUBDATE'],5,11); 34. echo("<p>Latest update: <a href=\"{$item['LINK']}\">"); 35. echo($fDate); 36. echo("</a></p>"); 37. break; 38. } 39. ?> Not the cleanest solution by far, but it will likely work just fine. Quote Link to comment https://forums.phpfreaks.com/topic/203772-limiting-the-number-of-results-displayed/#findComment-1067261 Share on other sites More sharing options...
OhTheNoes Posted June 3, 2010 Author Share Posted June 3, 2010 Fantastic. It works perfectly! Thank you very much! Quote Link to comment https://forums.phpfreaks.com/topic/203772-limiting-the-number-of-results-displayed/#findComment-1067265 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.