How to display a WordPress RSS feed on my website?

Hi, I have a website and a blog, I want to show my own Wordpress blog on my website.

  • I want to show only 3 posts on my site.
  • I want to automatically check for any new entry every time I reload my website so that only the latter are displayed only.
  • I want to show the full name of my Wordpress blog, but the specific letters of the description.
  • Also, the description should contain a word, and not part of a dictionary that does not contain a word ending with the words "..."

How can this be done, I heard that it can be done via RSS. Can someone help me?

+6
source share
3 answers

To do this, you need to read the RSS blog, from RSS you need to read the title and description, after reading the entire description and title, you need to trim the description to the desired number of letters. After that, you need to check the weather in the description of the last word that was completed or not, and then you need to delete the last word if it was not completed, and put "...".

First we will make a script to crop the description and put "..." in the 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 create an array and store the values โ€‹โ€‹in it. I accept only 3 because you asked about it. You can change it to anything (the number of messages you want to show, put in a 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, the link is the value that your user will click on and he will be sent to your blog: -

FIRST LAST MAIL:

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

SECOND LAST MAIL:

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

THIRD LAST MAIL:

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

Hope this helps solve your problem. By the way, a good question.

+5
source

Click here for source documentation on showing RSS feeds with PHP.

The Django Anonymous substrwords used to trim the description and paste ... at the end of the description if it passes the value of $maxchar .


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 invoke it on your actual page.

Example:

social.php

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

In addition, this code is compatible with plug-n-play.

+3
source

Why not use the Wordpress REST API to retrieve messages -

API URL: https://public-api.wordpress.com/rest/v1/sites/ $ site / posts /

where $ site is the site id of your wordpress blog

or just use this plugin -

http://www.codehandling.com/2013/07/wordpress-feeds-on-your-website-with.html

0
source

Source: https://habr.com/ru/post/908127/


All Articles