开发者

How to Retrieve Blogposts (Wordpress) From External Script

How do I go about retr开发者_如何转开发ieving all of the blogposts from my Wordpress blog via an external PHP script? Is this even possible? I have seen the API for creating a Wordpress plugin, but I'm not sure if that is relevant in this particular case. Any suggestions are greatly appreciated. Thank you.


Your external script can load the wordpress api with

include('blog/wp-load.php'); // change blog/ to your actual path

Then you can use get_posts or query_posts to get the posts you want.


Wordpress has a feed that your posts get published to by default. You can read the XML feed, and parse out the relevant data.

I've got a vanity site that I use to send clients to, and I also contribute occasionally to a blog. One of the things that my vanity site shows is a short list of links to the top 5 most recent posts from the blog. Here's the code I use to do it:

<ul>
<?php
    //slurp latest post from Wordpress' RSS feed, and cache them for short time.
    $posts = '';
    $cachefile = 'my-blog-cache-file.html';
    if (is_readable($cachefile) && filemtime($cachefile) > (time() - 1800)) {
        readfile($cachefile);
    }
    else {
        $doc = new DOMDocument();
        $doc->load('http://my.wordpress.blog/feed');
        $items = $doc->getElementsByTagName('item');
        foreach($items as $i)
        {
            if ($i->hasChildNodes()) {
                $title = $link = '';
                foreach($i->childNodes as $cn) {
                    if ($cn->nodeName == 'title') $title = $cn->nodeValue;
                    if ($cn->nodeName == 'link') $link = $cn->nodeValue;
                    if ($cn->nodeName == 'dc:creator') $author = $cn->nodeValue;
                }
                if ($title != '' && $link != '' && $author == 'my name') {
                    $posts .= '<li><a href="'.$link.'">'.$title.'</a></li>'."\n";
                }
            }
        }
        file_put_contents($cachefile,$posts);
        echo $posts;
    }
?>
</ul>

Feel free to use this code. You can examine the feed of your own blog and decide what elements you want to parse out. Generally your feed will be located at your blog's URL, with /feed tacked onto the end.


The other alternative of course is to use PHP to connect to your database and read the database yourself :)

//You'll want to set your database credentials 
mysql_connect($server, $username, $password);
mysql_select_db($wp_db);

// Modify the fields to pull whatever data you need for the output, even perhaps join the wp_users table for user data // Setting the ORDER BY to DESC to mimic the Wordpress ordering with newest first $sql = "SELECT ID, post_author, post_date, post_content, post_title, post_status, post_name, guid FROM wp_posts ORDER BY post_date DESC"; $data = mysql_query($sql);

$num = count($data); for($i = 0; $i < $num; $i++){ $row = mysql_fetch_array($data); // Output your posts to something print_r($row); }

This should allow you to play with the data far more easily :)


You'll want to take a look at Magpie. It's a fairly straight-forward RSS client for PHP, which let's you subscribe to any feed and get the posts with just a few lines of code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜