You can use MySQL query snippet below if you want to display your most recently published Wordpress posts on another web site or a different WP blog which reside in same or another server, by expressing the publishing or modifying date, the title, content or the author. But before proceeding to the solution, let’s glance at the standart methods that have disadvantages which you might have refered as alternative solutions.
In order to achieve the intended purpose, you might have thought to use Wordpress Loop which is a powerfull tool indeed. Either you manage it with the standart query_posts or with WP_Query object which you can customize your own loops, you’ll face two important problems;
/* You need to call wp-load.php file from your blog (e.g.. www.yourdomain.com/blog)
* to gain access to Wordpress blog content from external sites. You shall not need
* this section of snippet when you use or include it inside your blog pages.
*/
<?php
// Include Wordpress
define('WP_USE_THEMES', false);
require('./blog/wp-load.php');
?>
/* WP Loop */
<?php
$recentPosts = new WP_Query();
$recentPosts->query('showposts=1');
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
Last Updated: <?php the_modified_date(); ?><br>
<?php the_title(); ?><br>
Author: <?php the_author(); ?><br>
<a href="<?php the_permalink(); ?>"><img border=0 src="http://www.yourdomain.com/images/more.jpg" alt="more" width="60" height="18" /></a><br>
<?php endwhile; ?>
<?php rewind_posts(); ?>
First one of the problems is if you want to display your most recent posts as a preview on both a static web page and on the blog area (e.g. blog frontend) which your posts actually live, you’ll realize that the preview is being showed correctly on static pages but not inside the blog areas as a matter of the nature of WP Loop. Contrarily you’ll see that older posts are listed right behind your recent post. As in the example above even you use the rewind_posts directive for recovering the situation, whenever you enter directly to a spesific post in your blog you’ll realize that the post itself shows up that you’re viewing inside the preview section instead of the recent one. The second problem is if you want to display the recent post of your blog inside a totally different blog, you’ll not be able to communicate with the other blog as looping blog only processes the operation inside its own database.
In order not to experience all these troubles I recommend you to use MySQL query snippet I prepared. In this way you can have your recent post displayed in any place in any design format as you wish. You can use this query placing it into any proper location that you choose inside you pages.
<?php
$host="localhost";
$username="username";
$password="password";
$database="wp_db_name";
mysql_connect($host,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
/* WP MySQL query */
$query = "SELECT *, DATE_FORMAT(post_date, '%Y-%m-%d %k:%i') AS post_date, DATE_FORMAT(post_modified, '%Y-%m-%d %k:%i') AS post_modified FROM wp_posts WHERE post_status = 'publish' ORDER BY id DESC LIMIT 1";
$result = mysql_query ($query) or die (mysql_error());
while ($row = mysql_fetch_array($result))
{
$date = $row['post_date'];
$modified = $row['post_modified'];
$title = $row['post_title'];
$excerpt = $row['post_excerpt'];
$id = $row['post_author'];
$link = $row['guid'];
}
$query2 = "SELECT display_name FROM wp_users WHERE ID=$id";
$result2 = mysql_query ($query2) or die (mysql_error());
while ($row2 = mysql_fetch_array($result2))
{
$author = $row2['display_name'];
}
?>
Last Updated: <?php echo "$modified"; ?><br>
<?php echo "$title"; ?><br>
Author: <?php echo "$author"; ?><br>
<a href="<?php echo "$link"; ?>"><img border=0 src="http://www.yourdomain.com/images/more.jpg" alt="more" width="60" height="18" /></a>
Certainly you may change the output by using $date instead of $modified or including $excerpt variable in your configuration. Also you may enrich the output visually by embedding these <?php echo commands inside the various table designs.
Related Posts
No related posts.







RSS feed for comments on this post.




