Wordpress RSS shortcode


/ Published in: PHP
Save to your folder(s)

shortcode :
[rss size="10" feed="http://wordpress.org/news/feed/" date="true"]


Copy this code and paste it in your HTML
  1. /*
  2. * Re-usable RSS feed reader with shortcode
  3. */
  4. if ( !function_exists('base_rss_feed') ) {
  5. function base_rss_feed($size = 5, $feed = 'http://wordpress.org/news/feed/', $date = false, $cache_time = 1800)
  6. {
  7. // Include SimplePie RSS parsing engine
  8. include_once ABSPATH . WPINC . '/feed.php';
  9.  
  10. // Set the cache time for SimplePie
  11. add_filter( 'wp_feed_cache_transient_lifetime', create_function( '$a', "return cache_time;" ) );
  12.  
  13. // Build the SimplePie object
  14. $rss = fetch_feed($feed);
  15.  
  16. // Check for errors in the RSS XML
  17. if (!is_wp_error( $rss ) ) {
  18.  
  19. // Set a limit for the number of items to parse
  20. $maxitems = $rss->get_item_quantity($size);
  21. $rss_items = $rss->get_items(0, $maxitems);
  22.  
  23. // Store the total number of items found in the feed
  24. $i = 0;
  25. $total_entries = count($rss_items);
  26.  
  27. // Output HTML
  28. $html = "<ul class='feedlist'>";
  29. foreach ($rss_items as $item) {
  30. $i++;
  31.  
  32. // Add a class of "last" to the last item in the list
  33. if( $total_entries == $i ) {
  34. $last = " class='last'";
  35. } else {
  36. $last = "";
  37. }
  38.  
  39. // Store the data we need from the feed
  40. $title = $item->get_title();
  41. $link = $item->get_permalink();
  42. $desc = $item->get_description();
  43. $date_posted = $item->get_date('d/m/Y');
  44. //$desc = strip_tags($desc);
  45. $desc =wp_kses(trim($desc),array());
  46. $desc = apply_filters('the_excerpt', $desc);
  47.  
  48. $desc = strip_tags($desc,"");
  49. //print(' --- strlen : '.strlen($desc));
  50. $excerpt_length = 100;
  51. $words = explode(' ', $desc, $excerpt_length + 1);
  52. //print(' --- words : '.count($words));
  53. if(count($words) > $excerpt_length) :
  54. array_pop($words);
  55. array_push($words, '...');
  56. $desc = implode(' ', $words);
  57. endif;
  58. $desc = trim($desc);
  59.  
  60. while(strpos($desc, ' ') !== false)
  61. $desc = str_replace(' ', ' ', $desc); //eventually $s will == '/'
  62. $desc = substr(($desc),10);
  63. // Output
  64. $html .= "<li id='post-$i'$last>";
  65. $html .= "<h4><a href='$link'>$title</a></h4>";
  66. if( $date == true ) $html .= "<span class='date'>$date_posted</span>";
  67. $html .= "<div class='rss-entry'>$desc</a></h4></div>";
  68. $html .= "</li>";
  69. }
  70. $html .= "</ul>";
  71.  
  72. } else {
  73.  
  74. $html = "An error occurred while parsing your RSS feed. Check that it's a valid XML file.";
  75.  
  76. }
  77.  
  78. return $html;
  79. }
  80. }
  81. /** Define [rss] shortcode */
  82. if( function_exists('base_rss_feed') && !function_exists('base_rss_shortcode') ) {
  83. function base_rss_shortcode($atts) {
  84. extract(shortcode_atts(array(
  85. 'size' => '10',
  86. 'feed' => 'http://wordpress.org/news/feed/',
  87. 'date' => false,
  88. ), $atts));
  89.  
  90. $content = base_rss_feed($size, $feed, $date);
  91. return $content;
  92. }
  93. add_shortcode("rss", "base_rss_shortcode");
  94. }

URL: http://www.kevinleary.net/display-rss-feeds-wordpress-shortcodes-simplepie-fetch_feed/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.