tags:

views:

197

answers:

4

Hi friends,

I have a commercial site (php), and have a Wordpress blog in a subdirectory. I need to display latest posts at homepage which is out of Wordpress :/

site: http://www.blabla.com

blog: http://www.blabla.com/blog/

So I need to display posts at www.blabla.com/index.php. How can I access Wordpress functionality?

Thanks a lot! appreciate!

A: 

The easiest way is to consume your Wordpress RSS feed.

Download it using file_get_contents() or cURL for more control.

Parse it with simpleXML and output it.

You'll probably want to cache it somewhere... you could use APC user functions or PEAR::Cache_Lite.

Edit: the code would look something like this (you'd want more error checking and stuff - this is just to get you started):

$xmlText = file_get_contents('http://www.blabla.com/blog/feed/');

$xml = simplexml_load_string($xmlText);

foreach ($xml->item as $item)
{
    echo 'Blog Post: <a href="' . htmlentities((string)$item->link) . '">'
        . htmlentities((string)$item->title) . '</a>';

    echo '<p>' . (string)$item->description . '</p>';
}
Greg
uuu it doesnt sounds so easy actually : ) many steps... I'm looking into details of your advise, thanks!
artmania
@artmania: Since your main site and your blog are on the same server and both use php, this technique is probably unnecessary, though in some ways it is a bit more flexible than what you are doing
Brian
A: 

I guess the easiest solution is to take posts directly from database.

FractalizeR
It's risky to go poking around another programmes internals - you're one wordpress upgrade away from disaster...
Greg
+1  A: 

hey just found a solution online;

http://www.corvidworks.com/articles/wordpress-content-on-other-pages

works great!

<?php
// Include Wordpress 
define('WP_USE_THEMES', false);
require('blog/wp-blog-header.php');
query_posts('showposts=3');


?>   
<?php while (have_posts()): the_post(); ?>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php endwhile; ?>
artmania
+1  A: 

Using WordPress best practices, you shouldn't be loading wp-blog-header.php, but rather wp-load.php, as it was specifically created for this purpose.

After this, use either the WP_Query object or get_posts(). An example of how to use WP_Query is available on The Loop page on the WordPress codex. Although using either of these doesn't matter if you use them from outside WordPress, there's less chance of something interfering, such as GET parameters.

For example, using WP_Query:

<?php
$my_query = new WP_Query('showposts=3');
while ($my_query->have_posts()): $my_query->the_post();
?>
<h1><a href="<?php the_permalink() ?>"><?php the_title() ?></a></h1>
<?php endwhile; ?>

Or, using get_posts():

<?php
global $post;
$posts = get_posts('showposts=3');
foreach($posts as $post) :
?>
<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
<?php endforeach; ?>

Hope this helps! :)

Ryan McCue