MariaL
MariaL

Reputation: 1242

Show blog posts on non-wordpress site with php

I'm trying to display the most recent blog post on the homepage of a website that isn't wordpress, using php.

This is my code:

<?php require('../../../../../../../blog/wp-blog-header.php');

?>

<?php   
$args = array( 'numberposts' => 3, 'post_status'=>"publish",'post_type'=>"post",'orderby'=>"post_date");
$postslist = get_posts( $args );
echo '<ul id="latest_posts">';
 foreach ($postslist as $post) :  setup_postdata($post); ?> 
    <li><strong><?php the_date(); ?></strong><br />
    <a href="<?php the_permalink(); ?>" title="<?php the_title();?>"> <?php the_title(); ?></a>
</li>
<?php endforeach; ?>
 </ul> 

But I'm getting this error:

Fatal error: require(): Failed opening required '../../../../../../../blog/wp-blog-header.php' (include_path='/home/devbh/public_html/app/code/local:/home/devbh/public_html/app/code/community:/home/devbh/public_html/app/code/core:/home/devbh/public_html/lib:.:/usr/lib/php:/usr/local/lib/php') in /home/devbh/public_html/app/design/frontend/test1/default/template/page/hpBlog.phtml on line 35

I've made sure the filepath is correct so that shouldn't be the issue, but can't really think what else it could be? Any ideas?

Upvotes: 0

Views: 1565

Answers (1)

chickahoona
chickahoona

Reputation: 2034

Some example code how it should work:

<?php
// those two lines at the very top
define('WP_USE_THEMES', false);
require('/path/to/your/wordpress/wp-load.php');

// ... rest of your code like for example ...

query_posts('showposts=1');
while (have_posts()): the_post(); ?>

<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<p><a href="<?php the_permalink(); ?>">Read more...</a></p>

<?php endwhile; ?>

Here is an article with some further details:

https://wordpress.org/support/topic/display-wordpress-content-outside-of-your-blog-corvid-works

If your error persists, and both your sites run on different virtual hosts, then check your apache configuration. Make sure your vhost can access your other directory (from a security perspective its kindof nasty to do that)

Upvotes: 1

Related Questions