show wordpress post outside non wordpress php page

I need to display the wordpress blog posts in a non wordpress php page. I have tried the following code.

<?php
// Include WordPress
define('WP_USE_THEMES', false);
//exact path for wp-load.php.
// This file is kept in the root of wordpress install
require('http://test.com/wordpress/blog/wp-load.php');
//Query wordpress for latest 4 posts
query_posts('showposts=5');
?>
<?php while (have_posts ()): the_post(); ?>
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>

<?php endwhile; ?>

But it showed me the following error

Read More
Fatal error: Call to undefined function query_posts()

How to fix this?

Related posts

Leave a Reply

6 comments

  1. please look into the following line in your code

    require('http://test.com/wordpress/blog/wp-load.php');
    

    in the require function you should use the relative or physical path. You should not include the url.

  2. Since you need the wordpress database and framework this seems very unlikely to work at all. Try getting XML, RSS or JSON data from your wordpress which you can fetch with a self-made script.

  3. For external integration, it’s likely more reliable to approach this via RSS route. Simplest and possibly laziest way of making this work is by use of simplexml_load_file (and HTTP streams.)

    $t = simplexml_load_file( "http://blogs.voanews.com/breaking-news/feed/" );
    
    foreach( $t->channel->item as $item ) {
        printf(
            "<div>%s <a href='%s'>%s</a></div><hr/>",
            $item->description,
            $item->link,
            $item->title
        );
    }
    

    This outputs the feed as you’d expect to see it. Note that this doesn’t use any kind of caching, so every page request hits the original feed.

    <div>Some Chinese officials are furious at Apple's iPhone for apparently 
    helping users have too much of a good time. Chinese media say the complaints
    surround the iPhone's voice-activated personal assistant, known as
    “Siri,” which has been helping some users find prostitutes and
    brothels. The Mandarin language version can apparently present users with as
    many as [...] <a href='...(snip)...)'>iPhone Under Fire in China over
    Prostitution</a></div>
    
  4. You could use the rss option, you could write a new, hidden code and read data using that file … in JSON format

    Maybe the first thing to do is search for a extention/plugin/module that does this for you;

    You are not the first one who wanted to do this, i think :p

  5. For Displaying recent posts outside wordpress setup, first include wp-load.php file.

    require( './blog/wp-load.php' );
        // Load the recent top 10 posts
        $args = array( 'posts_per_page' => 10, 'post_status'=>"any", 'post_type'=>"post", 'orderby'=>"date","order"=> "DESC", "suppress_filters"=>true);
        $postslist = get_posts( $args );
    

    Now you can loop through $postlist variable.

    foreach ($postslist as $post) : setup_postdata($post);?> 
          <ul class="media-list">
            <li class="media">
              <div class="media-left"> <?php the_post_thumbnail( array(80,80));?> </div>
              <div class="media-body">
                <a target="_blank" href="<?php the_permalink();?>"><h5 class="media-heading"><?php the_title(); ?></h5></a>
                <p><?php echo $post->post_content; ?></p>
              </div>
            </li>
          </ul>
    
    <?php endforeach;
    
  6. After you include the wp-load.php you have to instantiate the query like this:

    $wp_query = new WP_Query();
    $wp_query->query('showposts=5');
    

    After that your loop should look like this:

    <?php while ($wp_query->have_posts()) :
        $wp_query->the_post();
        <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
        <?php the_excerpt(); ?>
    
    <?php endwhile; ?>