PHP Wordpress RSS短代码

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP Wordpress RSS短代码相关的知识,希望对你有一定的参考价值。

/*
*	Re-usable RSS feed reader with shortcode
*/
if ( !function_exists('base_rss_feed') ) {
	function base_rss_feed($size = 5, $feed = 'http://wordpress.org/news/feed/', $date = false, $cache_time = 1800)
	{
		// Include SimplePie RSS parsing engine
		include_once ABSPATH . WPINC . '/feed.php';

		// Set the cache time for SimplePie
		add_filter( 'wp_feed_cache_transient_lifetime', create_function( '$a', "return cache_time;" ) );

		// Build the SimplePie object
		$rss = fetch_feed($feed);

		// Check for errors in the RSS XML
		if (!is_wp_error( $rss ) ) {

			// Set a limit for the number of items to parse
			$maxitems = $rss->get_item_quantity($size);
			$rss_items = $rss->get_items(0, $maxitems);

			// Store the total number of items found in the feed
			$i = 0;
			$total_entries = count($rss_items);

			// Output HTML
			$html = "<ul class='feedlist'>";
			foreach ($rss_items as $item) {
				$i++;

				// Add a class of "last" to the last item in the list
				if( $total_entries == $i ) {
					$last = " class='last'";
				} else {
					$last = "";
				}

				// Store the data we need from the feed
				$title = $item->get_title();
				$link = $item->get_permalink();
				$desc = $item->get_description();
				$date_posted = $item->get_date('d/m/Y');
                //$desc = strip_tags($desc);
                $desc =wp_kses(trim($desc),array());
                $desc = apply_filters('the_excerpt', $desc);

                $desc = strip_tags($desc,"");
                //print(' --- strlen : '.strlen($desc));
                $excerpt_length = 100;
                $words = explode(' ', $desc, $excerpt_length + 1);
                //print(' --- words : '.count($words));
                if(count($words) > $excerpt_length) :
                    array_pop($words);
                    array_push($words, '...');
                    $desc = implode(' ', $words);
                endif;
                $desc = trim($desc);

                while(strpos($desc, '  ') !== false)
                    $desc = str_replace('  ', ' ', $desc); //eventually $s will == '/'
                $desc = substr(($desc),10);
				// Output
				$html .= "<li id='post-$i'$last>";
				$html .= "<h4><a href='$link'>$title</a></h4>";
				if( $date == true ) $html .= "<span class='date'>$date_posted</span>";
				$html .= "<div class='rss-entry'>$desc</a></h4></div>";
				$html .= "</li>";
			}
			$html .= "</ul>";

		} else {

			$html = "An error occurred while parsing your RSS feed. Check that it's a valid XML file.";

		}

		return $html;
	}
}
/** Define [rss] shortcode */
if( function_exists('base_rss_feed') && !function_exists('base_rss_shortcode') ) {
	function base_rss_shortcode($atts) {
		extract(shortcode_atts(array(
			'size' => '10',
			'feed' => 'http://wordpress.org/news/feed/',
			'date' => false,
		), $atts));

		$content = base_rss_feed($size, $feed, $date);
		return $content;
	}
	add_shortcode("rss", "base_rss_shortcode");
}

以上是关于PHP Wordpress RSS短代码的主要内容,如果未能解决你的问题,请参考以下文章

PHP RSS转换为HTML - Wordpress

PHP 从Wordpress类别页面中删除RSS源

PHP Wordpress:在RSS源中注入缩略图

PHP 通过functions.php禁用WordPress RSS-Feed

php 将特色图像添加到Wordpress RSS feed

PHP Wordpress将帖子缩略图添加到RSS源