Show WordPress posts on another site

Lets see how we can show wordpress posts on another site. For instance you have blog installed under a sub directory as “blog”. Your main site is parentsite.com. The main site should have .php extension as we are going to include wordpress header in it using php command. Now lets move to sharing wordpress posts on it.

Show wordpress posts on another site step 1

Open the page of you main site you want show WordPress posts on. Lets say it is account.php. Now open this php and paste the following code to get the header of wordpress.

<?php
/* Short and sweet */
define(‘WP_USE_THEMES’, false);
require(‘blog/wp-blog-header.php’);
?>

After Loading the Liabraray

Past the following code after getting the header.

<?php
// Get the last 3 posts.
global $post;
$args = array( ‘posts_per_page’ => 5 );
$myposts = get_posts( $args );

foreach( $myposts as $post ) :    setup_postdata($post); ?>
<li class=”menu-item”><a href=”<?php the_permalink() ?>” rel=”bookmark” title=”Permanent Link to <?php the_title_attribute(); ?>”><?php the_title(); ?></a></li>
<?php endforeach; ?>

That’s it. You can show WordPress posts the recent five posts from your WordPress blog on you external site. You can Show WordPress posts for any number on another site by just changing number against the “numberposts” variable.

Key Points

In order to show WordPress posts correctly the WordPress path must be given correctly. Look at the code below. The blog at this site is installed in the folder blog. Further we want to show WordPress posts in the form of list. So we have changed the code a bit and included <li> and </li> in the code.

Show WordPress posts

The final result on the website is as follow:

Show WordPress posts

If you want to show WordPress posts in five posts sorted alphabetically in ascending order on your web page, you could do the following to grab the posted date, title and excerpt:

<?php
$posts = get_posts('numberposts=10&order=ASC&orderby=post_title');
foreach ($posts as $post) : setup_postdata( $post ); ?>
<?php the_date(); echo "<br />"; ?>
<?php the_title(); ?>    
<?php the_excerpt(); ?> 
<?php
endforeach;
?>

Remember this all code comes after you have included the WordPress header. For full information you can also go to the official WordPress tutorial on Integrating WordPress with Your Website.

One Response to “Show WordPress posts on another site”

  1. Thank you!
    You might want to change the ‘ and ” in your code – they’re the wrong ones and throw errors.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *