Jump to content

Limiting the number of results displayed


OhTheNoes

Recommended Posts

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.
       ?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.