Problems Adding Wordpress Feed to Website

Discussion in 'WordPress' started by dcristo, Oct 19, 2006.

  1. #1
    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.
     
    dcristo, Oct 19, 2006 IP
  2. cpucandy

    cpucandy Peon

    Messages:
    489
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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.
     
    cpucandy, Oct 19, 2006 IP
  3. dcristo

    dcristo Illustrious Member

    Messages:
    19,776
    Likes Received:
    1,200
    Best Answers:
    7
    Trophy Points:
    470
    Articles:
    7
    #3
    Have you got a link mate?
     
    dcristo, Oct 19, 2006 IP
  4. cpucandy

    cpucandy Peon

    Messages:
    489
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #4
    cpucandy, Oct 19, 2006 IP
  5. dcristo

    dcristo Illustrious Member

    Messages:
    19,776
    Likes Received:
    1,200
    Best Answers:
    7
    Trophy Points:
    470
    Articles:
    7
    #5
    Cool thanks. Have you got example URL's of where you have used it?
     
    dcristo, Oct 19, 2006 IP
  6. cpucandy

    cpucandy Peon

    Messages:
    489
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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.
     
    cpucandy, Oct 19, 2006 IP
    dcristo likes this.
  7. dcristo

    dcristo Illustrious Member

    Messages:
    19,776
    Likes Received:
    1,200
    Best Answers:
    7
    Trophy Points:
    470
    Articles:
    7
    #7
    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?
     
    dcristo, Oct 19, 2006 IP
  8. cpucandy

    cpucandy Peon

    Messages:
    489
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #8
    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. :)
     
    cpucandy, Oct 19, 2006 IP
    highflyer likes this.
  9. dcristo

    dcristo Illustrious Member

    Messages:
    19,776
    Likes Received:
    1,200
    Best Answers:
    7
    Trophy Points:
    470
    Articles:
    7
    #9
    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.
     
    dcristo, Oct 19, 2006 IP
  10. cpucandy

    cpucandy Peon

    Messages:
    489
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #10
    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.
     
    cpucandy, Oct 19, 2006 IP
  11. dcristo

    dcristo Illustrious Member

    Messages:
    19,776
    Likes Received:
    1,200
    Best Answers:
    7
    Trophy Points:
    470
    Articles:
    7
    #11
    dcristo, Oct 19, 2006 IP
  12. cpucandy

    cpucandy Peon

    Messages:
    489
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #12
    The description? Or do you mean the body of the article from the feed?
     
    cpucandy, Oct 19, 2006 IP
  13. dcristo

    dcristo Illustrious Member

    Messages:
    19,776
    Likes Received:
    1,200
    Best Answers:
    7
    Trophy Points:
    470
    Articles:
    7
    #13
    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?
     
    dcristo, Oct 19, 2006 IP
  14. cpucandy

    cpucandy Peon

    Messages:
    489
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #14
    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.
     
    cpucandy, Oct 19, 2006 IP
  15. dcristo

    dcristo Illustrious Member

    Messages:
    19,776
    Likes Received:
    1,200
    Best Answers:
    7
    Trophy Points:
    470
    Articles:
    7
    #15
    Thanks a lot mate. I owe you a beer or two or three!! :)
     
    dcristo, Oct 19, 2006 IP
  16. cpucandy

    cpucandy Peon

    Messages:
    489
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Looks like your up and running. Thanks for the green!
     
    cpucandy, Oct 20, 2006 IP