WordPress posts on non-wordpress pages?

WordPress posts on non-wordpress pages?

This post will show you how to get the latest or any other array of posts based on a set of parameters using get_posts into a non-wordpress based page

Firstly create a new template file in your WordPress theme – call it latest.php and put in it the following:

<?php
/*
Template Name: Latest
*/
?>

        <div id="blog">

<?php
global $post;
$myposts = get_posts('numberposts=20');
foreach($myposts as $post) :
setup_postdata($post);
?>

            <div>
                <h2><a data-link="<?php the_permalink(); ?>" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                <p><?php the_excerpt(); ?></p>
                <div>
                    <span><?php the_date(); ?></span>
                    <span><a data-link="<?php the_permalink(); ?>" href="<?php the_permalink(); ?>">read more</a></span>
                </div>
            </div>

<?php endforeach; ?>

            </div>

If you want to tweak the parameters, follow the instructions on the WordPress Codex for the get_posts function. Here I am pulling in the last 20 posts. You can change the HTML within and add any further post related info using php. The above example will create a Masonry layout.

Next create a page within WordPress. Give it a title, e.g. Latest Posts and select the template for the page as Latest – this will use the file you created above. Don’t add any content into the page, just a title.

Lastly, go to your non-Wordpress page and add the following code:

<?php readfile("http://berkhamsted-web-design.co.uk/blog/latest-posts/"); ?>

You could also use include() instead of readfile() – depends on your webhost.

Oliver Partridge

Building websites since 2000, I have a wealth of experience in eMarketing, SEO, e-mail marketing and social media marketing. Google+ and Twitter

2 Comments

  1. Jason S says:

    Thanks this worked great! I wish there was a way to limit the post to only show so much of it in the “recent post section” Great work, this was exactly what I was looking for.

Leave a Reply


  • Linux alternative to Windows Server

    Linux

    This will be an on-going post of my research, tests and experiences of setting up and using a linux distribution with the aim to replace

  • 4 column wordpress theme4 column wordpress theme

    4 column wordpress theme

    Templates & Themes Website Design & Development Wordpress

    So you’ve got your WordPress blog up and running and have started to think about the look of it and themes? In this series we’ll

  • 10 Awesome Magento Themes

    Templates & Themes

    Magento is a great and feature packed eCommerce package, that works well off-the-shelf. Supercharge your online shop with a pre-built Magento theme, already to go