How To Embed WordPress Posts in a Website Page
March 28th, 2014Sometimes you want to display the last few posts of a WordPress blog on a regular website page, assuming the WordPress installation is in a sub directory of the website. This is an alternative to using an RSS to HTML method. I’m saving the code here for myself, and in the event it helps another developer.
<?php
define(‘WP_USE_THEMES’, false);
require(‘/home/username/yourdomain.com/blog/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 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><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.