Search code examples
wordpressrssxml-rpc

How to get posts' content as HTML from a wordpress blog remotely


I have a self-hosted WordPress blog, and I am making a static home page for my website based on jQuery. So, I wanted to display some content from my blog, on my home page (in widgets), as a news section.

For example, I may fetch

  • the latest five posts' titles and content
  • OR a specific page content (via passing the page id)
  • OR a specific post (via passing the post id)

So does WordPress include any PHP file that shows the posts contents as plain text, or HTML?

I thought about fetching the blog's RSS, and then show it on the page, but the RSS doesn't provide the full content of the post.


Solution

  • If it's hosted on the same server, you could integrate WordPress into your app by including wp-blog-header.php, and then call get_posts(), using setup_postdata().

    For example:

     <ul>
         <?php
             global $post;
             $tmp_post = $post;
             $myposts = get_posts('numberposts=5&offset=1&category=1');
             foreach($myposts as $post) :
                 setup_postdata($post);
         ?>
    
             <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    
             <?php endforeach; ?>
         <?php $post = $tmp_post; ?>
     </ul>