Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Show latest WordPress blog posts on homepage

Posted on 2007-11-23
5
Medium Priority
?
1,571 Views
Last Modified: 2012-05-05
Hi

I have recently installed a WordPress blog, and I would like to show the latest 4/5 posts on my homepage (outside of the blog) is there any easy way in PHP I can fetch the latest posts?

Thanks

John

0
Comment
Question by:logicjb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 

Author Comment

by:logicjb
ID: 20338563
Thanks, but this code only works from within wordpress. I need an external solution to call the latest posts (same server)
0
 
LVL 4

Accepted Solution

by:
simonkin earned 2000 total points
ID: 20338764
Hi,

Try this...


<?php
 
	// Connect to your database
	mysql_connect("db-host", "db-user", "db-password")or die("cannot connect");
	mysql_select_db("db-name")or die("cannot select DB");
	
	// How many posts to show?
	$show = 5;
	
	// Set the query
	$query = "SELECT id, post_content, post_title, post_name FROM wp_posts WHERE post_status='publish' ORDER BY id DESC LIMIT 0,$show";
	$result = mysql_query($query);
	
	// Build an array of posts
	$posts = array();
	
	// Iterate through the posts and populate the array
	while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
	
		$id = $row["id"];
		$posts[$id]["id"] = $row["id"];
		$posts[$id]["content"] = $row["post_content"];
		$posts[$id]["title"] = $row["post_title"];
		$posts[$id]["link"] = $row["post_name"];
	
	}
	
	// Close the database connection if you don't have a persistant connection...
	mysql_close();
 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
 
<body>
<table width="100%" border="0" cellspacing="2" cellpadding="2">
  <tr>
    <td width="10%" align="left" valign="top"><strong>ID:</strong></td>
    <td width="28%" align="left" valign="top"><strong>Title:</strong></td>
    <td width="62%" align="left" valign="top"><strong>Post:</strong></td>
  </tr>
  
  <?php
  
  foreach ($posts as $post) {
  
  echo '
  <tr>
    <td align="left" valign="top">' . $post["id"] . '</td>
    <td align="left" valign="top"><a href="' . $post["link"] . '">' . $post["title"] . '</a></td>
    <td align="left" valign="top">' . $post["content"] . '</td>
  </tr>';
  
  }
  
  ?>
  
</table>
</body>
</html>

Open in new window

0
 

Author Closing Comment

by:logicjb
ID: 31410658
excellent, thanks
0
 
LVL 18

Expert Comment

by:Matthew Kelly
ID: 23061894
I know this question has been closed for a year so this is as an FYI for anyone else working on this. I wanted to include the latest posts on my home page (which happened to not be in wordpress). DO NOT RETRIEVE THIS KIND OF INFO DIRECTLY FROM THE WORDPRESS DATABASE.

A much better and easier method is to include the "wp-blog-header.php" file.

This would allow you to use Wordpress function calls on your PHP page. To figure out the correct wordpress function calls look at your theme editor pages. For example if I wanted to show recent posts, I look at my sidebar.php in the theme editor and grab out the section that displays recent posts.

Retrieving directly from the wordpress database is NOT recommended. It is not very upgrade friendly. The best method is to use the methods wordpress supplies to retrieve its information, so that as it increases in versions, everything is compliant.
<?php 
require('blog/wp-blog-header.php'); 
?>
<li><h1>Recent Posts</h1><ul><?php wp_get_archives('type=postbypost&limit=5')?></ul></li>

Open in new window

0

Featured Post

Understanding Web Applications

Without even knowing it, most of us are using web applications on a daily basis. Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We often confuse these web applications tools for websites.  So, what is the difference?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question