How to display latest posts on a non-WordPress page of your website
January 28th, 2015Sometimes websites have a WordPress blog installed in a sub directory, and you want to display the most recent posts on a page of the website that is not part of WordPress.
Here’s the PHP Code to do this.
Just put this inside a div and replace the path that’s after the require function to the full path of where ever your wordpress header file is.
<?php
define(‘WP_USE_THEMES’, false);
require(‘/home/something/yourwebsite.com/yourdirectory/wp-blog-header.php’);
// changes excerpt length from the default 55 to 20
function custom_excerpt_length( $length ) {
return 20;
}
add_filter( ‘excerpt_length’, ‘custom_excerpt_length’, 999 );
//*****************
// changes the default exerpt […] to a read more
function new_excerpt_more( $more ) {
return ‘…<em><a class=”read-more” href=”‘. get_permalink( get_the_ID() ) . ‘”>more</a></em>’;
}
add_filter( ‘excerpt_more’, ‘new_excerpt_more’ );
//*******************
global $post;
$args = array( ‘posts_per_page’ => 3 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) :   setup_postdata($post); ?>
<div class=”blogpost”><strong><a href=”<?php the_permalink() ?>” rel=”bookmark” title=”Permanent Link to <?php the_title_attribute(); ?>”><?php the_title(); ?></a></strong><br /><?php the_excerpt(); ?></div> <br />
<?php endforeach;
?>
--------------
J. Olkoski
Aldebaran Web Design, Seattle
Jill Olkoski has a BS in Engineering, a BS in Computer Science and an MA in Clinical Psychology. She delights in using her advanced technical and psychological skills to help small business owners develop cost-effective and successful websites.