Archive for the ‘PHP’ Category

WordPress blog contents to external page

Monday, April 18th, 2011

This blog’s content is created and running with WordPress, but other content in my web site is not. So I needed a way to export latest blog post titles from the WordPress’ MySql database to other (non-WordPress) pages.

Here’s a PHP snippet for that:

$connection = mysql_connect(“localhost”, “database_user”, “database_password”);
  1. mysql_select_db("database_table", $connection);
  2.  
  3. $query = "SELECT post_status, post_date, guid, post_title FROM wp_posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 0, 6";
  4.  
  5. $result = mysql_query($query, $connection) or die("Query error!");
  6.  
  7. echo "
  8.  
  9.  
  10. <h3>Blog updates:</h3>
  11.  
  12. ";
  13.  
  14. for ($i = 0; $i &lt; mysql_num_rows($result); $i++) {
  15.    $link = mysql_result($result, $i, "guid");
  16.    $title = mysql_result($result, $i, "post_title");
  17.  
  18.    echo "
  19.  
  20. <a href="$link">$title</a>
  21.  
  22. ";
  23. }
  24.  
  25. mysql_close($connection);

Just edit “database_user”, “database_password” and “database_table” to correspond with your WordPress settings and paste the code to a web page you want.

You can see this code working in a sidebar of the other pages on this site.