How to display WordPress RSS feed your website?

Hello i have a website and a blog, i want to display my self hosted wordpress blog on my website.

  1. I want to show only 3 post on my website.
  2. I want to automatically check for any new post everytime when i reload my website, so that the recent three gets displayed only.
  3. I want to show the complete title of my wordpress blogpost but specific letters of description.
  4. Also the description should end up with a word not some piece of non-dictionary word ending with “…”

How this can be done, i have heard that it can be done through RSS.
Can somebody help me?

Related posts

Leave a Reply

3 comments

  1. To accomplish this you need to read the RSS of the blog, from RSS you need to read the Title and the description, after reading the whole description and title you need to trim the description to your desired number of letters. After that you need to check weather the description last word has been completed or not and then you need to remove a the last word if not completed and put the “…”.

    First we will make a script to trim the description and to put “…” in last:-

    <?php
    global $text, $maxchar, $end;
    function substrwords($text, $maxchar, $end='...') {
        if (strlen($text) > $maxchar || $text == '') {
            $words = preg_split('/s/', $text);      
            $output = '';
            $i      = 0;
            while (1) {
                $length = strlen($output)+strlen($words[$i]);
                if ($length > $maxchar) {
                    break;
                } 
                else {
                    $output .= " " . $words[$i];
                    ++$i;
                }
            }
            $output .= $end;
        } 
        else {
            $output = $text;
        }
        return $output;
    }
    

    Now we will define the variables in which we store the values:-

    $xml=("http://your-blog-path/rss/");
    global $item_title, $item_link, $item_description;
    
    $xmlDoc = new DOMDocument();
    
    $xmlDoc->load($xml);
    
    $x=$xmlDoc->getElementsByTagName('item');
    

    Now, we will make an array and store values in it. I am only taking 3 because you have asked it the way. You can change it to anything (The number of post you want to show, put that in the loop)

    for ($i=0; $i<3; $i++)
    
    {
    
    $item_title[$i] = $x->item($i)->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;
    
    $item_link[$i] = $x->item($i)->getElementsByTagName('link')->item(0)->childNodes->item(0)->nodeValue;
    
    $item_description[$i] = $x->item($i)->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue;
    
    }
    
    ?>
    

    Now echo all these values, Link is the value where your user will click and he will be taken to your blog:-

    FIRST RECENT POST:

    <a href="<?php echo $item_link[0]; ?>"><?php echo $item_title[0]; ?></a>
    <?php echo substrwords($item_description[0],70); ?>
    

    SECOND RECENT POST:

    <a href="<?php echo $item_link[1]; ?>"><?php echo $item_title[1]; ?></a>
    <?php echo substrwords($item_description[1],70); ?>
    

    THIRD RECENT POST:

    <a href="<?php echo $item_link[2]; ?>"><?php echo $item_title[2]; ?></a>
    <?php echo substrwords($item_description[2],70); ?>
    

    Hope this can solve your problem. By the way Nice question.

  2. Click here for the original documentation on displaying RSS feeds with PHP.

    Django Anonymous’s substrwords function is being used to trim the description and to insert the ... at the end of the description if the it passes the $maxchar value.


    Full Code:

    blog.php

    <?php
        global $text, $maxchar, $end;
        function substrwords($text, $maxchar, $end='...') {
            if (strlen($text) > $maxchar || $text == '') {
                $words = preg_split('/s/', $text);      
                $output = '';
                $i      = 0;
                while (1) {
                    $length = strlen($output)+strlen($words[$i]);
                    if ($length > $maxchar) {
                        break;
                    } else {
                        $output .= " " . $words[$i];
                        ++$i;
                    }
                }
                $output .= $end;
            } else {
                $output = $text;
            }
            return $output;
        }
    
        $rss = new DOMDocument();
        $rss->load('http://wordpress.org/news/feed/'); // <-- Change feed to your site
        $feed = array();
        foreach ($rss->getElementsByTagName('item') as $node) {
            $item = array ( 
                'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
                'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
                'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
                'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
            );
            array_push($feed, $item);
        }
    
        $limit = 3; // <-- Change the number of posts shown
        for ($x=0; $x<$limit; $x++) {
            $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
            $link = $feed[$x]['link'];
            $description = $feed[$x]['desc'];
            $description = substrwords($description, 100);
            $date = date('l F d, Y', strtotime($feed[$x]['date']));
            echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
            echo '<small><em>Posted on '.$date.'</em></small></p>';
            echo '<p>'.$description.'</p>';
        }
    ?>
    

    You can easily put this in a separate PHP file (blog.php) and call it inside your actual page.

    Example:

    social.php

    <h3>Latest blog post:</h3>
    <?php require 'blog.php' ?>
    

    Also, this code is plug-n-play friendly.