Displaying latest wordpress post on ‘random’ Web Page
Our client’s brief was to display their latest blog post on their main web page in a truncated form, their main web page is not part or their blog. After a little searching I came across a wordpress plugin called Get-a-Post which seemed to promise just what we required.
It will retrieve a post, and sets the post’s data up correctly so that wordpress functions like the_title() and the_content() which must normally be called from within the wordpress loop can be called by php code that is outside of the wordpress loop, this is important to us as we want to display the data on a web page which is not part of the actual wordpress blog – from a ‘random’ page.
To install the plugin, just download it, and copy get-a-post.php to the blog’s wp-content/plugins/ directory. Then goto the wordpress admin/plugins screen and activate it.
Once the plugin has been activated, its function get_a_post() can be called from any php script, if called with no parameters the latest post will be made active – perfect! Before calling it make sure to include/require ‘wp-config.php’. Here is an example script that gets the latest post’s title and content and packages it up into a div and some spans:
<?php require_once(“./wp-config.php”); ?>
<?php get_a_post(); ?>
<span class=”last_post_title”><?php the_title()?></span> – <span class=”last_post_content”>
<?php echo get_the_content(); ?>