I am wanting to display the latest blog posts from my blog on a different website. Everything I have tried hasn't worked. Other feeds work fine so I know the RSS parser code I am using is ok. Here is the code I am using: {php} $insideitem = false; $tag = ""; $title = ""; $description = ""; $link = ""; $locations = array('http://nrgdomains.com/feed/'); srand((float) microtime() * 10000000); // seed the random gen $random_key = array_rand($locations); function startElement($parser, $name, $attrs) { global $insideitem, $tag, $title, $description, $link; if ($insideitem) { $tag = $name; } elseif ($name == "ITEM") { $insideitem = true; } } function endElement($parser, $name) { global $insideitem, $tag, $title, $description, $link; if ($name == "ITEM") { printf("<dt><b><a href='%s' target='new' rel='nofollow'>%s</a></b></dt>", trim($link),htmlspecialchars(trim($title))); printf("<dt>%s</dt><br><br>",htmlspecialchars(trim($description))); $title = ""; $description = ""; $link = ""; $insideitem = false; } } function characterData($parser, $data) { global $insideitem, $tag, $title, $description, $link; if ($insideitem) { switch ($tag) { case "TITLE": $title .= $data; break; case "DESCRIPTION": $description .= $data; break; case "LINK": $link .= $data; break; } } } $xml_parser = xml_parser_create(); xml_set_element_handler($xml_parser, "startElement", "endElement"); xml_set_character_data_handler($xml_parser, "characterData"); $fp = fopen($locations[$random_key], 'r') or die("Error reading RSS data."); $int = 0; while ($int != 1){ $data = fread($fp, 2056); xml_parse($xml_parser, $data, feof($fp)) or die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser))); $int++; } fclose($fp); xml_parser_free($xml_parser); {/php} Code (markup): I have attempted to use the different wordpress feed URL's outlined on this page http://codex.wordpress.org/WordPress_Feeds with no success. Any advice would be appreciated.
Have you tried using simplepie rss? I have it running on a couple of sites. Let me know if you would like some help with setting it up.
Sure, check out my reality tv site. You will see the feed on the front page. For this site I am using my own wordpress blog. You can also check out this site. I am grabbing a feed from a different website.
Thanks heaps, but it seems to be way over my head. Assuming I have installed the parser correctly, and just want to be able to display my wordpress blog feed on a different website which supports php, what code would I use on the page?
OK, add this code to the top of the page you want to display the feed on. Make sure it is above all of the html code on the page. If this is an html page, make sure you have the mime set to interpret html as php. <?php // Start counting time for the page load $starttime = explode(' ', microtime()); $starttime = $starttime[1] + $starttime[0]; // Include SimplePie // Located in the parent directory include('includes/simplepie.inc'); // Create a new instance of the SimplePie object $feed = new SimplePie(); // Set these Configuration Options $feed->bypass_image_hotlink(); $feed->strip_ads(true); // Make sure that page is getting passed a URL $feed->feed_url('http://www.thewebsite.com/feed/'); // Initialize the whole SimplePie object. Read the feed, process it, parse it, cache it, and // all that other good stuff. The feed's information will not be available to SimplePie before // this is called. $feed->init(); // We'll make sure that the right content type and character encoding gets set automatically. // This function will grab the proper character encoding, as well as set the content type to text/html. $feed->handle_content_type(); ?> Here is the additional code you will need to write out the feed entries. I suggest you take the web page you want to do this on a make a copy of it and rename it. You can test off of the new page. The below example will bring back the most recent 4. You can change get_item_quantity to whatever suits your needs: ------------------------------------------------------------ <!-- Let's begin looping through each individual news item in the feed. --> <?php $max = $feed->get_item_quantity(4); for ($x = 0; $x < $max; $x++) { $item = $feed->get_item($x); $description = ereg_replace("\"","'",$item->get_description()); ?> <!-- If the item has a permalink back to the original post (which 99% of them do), link the item's title to it. --> <p class="highlight"><?php if ($item->get_permalink()) echo '<a href="' . $item->get_permalink() . '">'; echo $item->get_title(); if ($item->get_permalink()) echo '</a>'; ?><br> Posted: <span class="footnote"><?php echo $item->get_date('j M Y'); ?></span><br> <!-- Display the item's primary content. --> <?php echo $description; ?> <!-- Stop looping through each item once we've gone through all of them. --> </p> <?php } ?> ----------------------------------------------------------------- Hope that helps you out.
ok great, and does this above code assume the file simplepie.inc is uploaded to the /includes/ folder in the root? I ask this because in the installation documentation it asks you to upload this file to the /php/ folder in the root directory.
Yes, the code assumes that the .inc file will be stored in an includes folder. It really doesn't matter where you keep it, just out of my past experiences I have always put include files in an includes folder. It is really up to you where you want to put it. If you want to change the location, you will need to modify the following line on your html page: include('includes/simplepie.inc'); Hope that helps.
Good on you mate, I got it going on the homepage of http://www.online-sellers-resource.com/ Do you know if there is a way to truncate the description for each blog post?
Umm, well I removed this bit of code <?php echo $description; ?> because it was displaying the entire article for each blog post, which was too long. I was wanting to know if you could reduce the length of each blog post in the feed?
Give this a try. It is using the php substr function. I just didn't test it out. Find this line: $description = ereg_replace("\"","'",$item->get_description()); ?> Change it to: $description = ereg_replace("\"","'",substr($item->get_description(),0,250)); ?> 0 is the starting point in the string. 250 is the number of characters. I would probably suggest that you add something like "..." to the end of the line so the reader knows to click on the link to read the full article. You can check out this link for an example of combing 2 strings: http://www.htmlite.com/php013.php I am going to bed now. I will get back to you tomorrow if you have any further questions.