Advertisement
pramodd

recent posts with thumbnail on non blog page

Apr 18th, 2011
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 2.05 KB | None | 0 0
  1. <?php
  2. //db parameters
  3. $db_username = 'XXXX';
  4. $db_password = 'XXXX';
  5. $db_database = 'XXXX';
  6. $db_host = 'XXXX';
  7.  
  8. $blog_url = 'http://mywebsite.com/blog/'; //base folder for the blog. Make SURE there is a slash at the end
  9.  
  10. //connect to the database
  11. mysql_connect($db_host, $db_username, $db_password);
  12. @mysql_select_db($db_database) or die("Unable to select database");
  13.  
  14. //get data from database -- !IMPORTANT, the "LIMIT 5" means how many posts will appear. Change the 5 to any whole number.
  15. $query = "Select * FROM wp_posts WHERE post_type='post' AND post_status='publish' ORDER BY id DESC LIMIT 3";
  16.  
  17. $query_result = mysql_query($query);
  18. $num_rows = mysql_numrows($query_result);
  19.  
  20. //close database connection
  21. mysql_close();
  22.  
  23. // html page starts after
  24. ?>
  25. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  26. <html xmlns="http://www.w3.org/1999/xhtml">
  27.  
  28. ...
  29.  
  30.  
  31. //start a loop that starts $i at 0, and make increase until it's at the number of rows
  32. for($i=0; $i< $num_rows; $i++){
  33.  
  34. //assign data to variables, $i is the row number, which increases with each run of the loop
  35. $blog_date = mysql_result($query_result, $i, "post_date");
  36. $blog_title = mysql_result($query_result, $i, "post_title");
  37. $blog_content = mysql_result($query_result, $i, "post_content");
  38. $blog_permalink = mysql_result($query_result, $i, "guid"); //use this line for p=11 format.
  39.  
  40. //$blog_permalink = $blog_url . mysql_result($query_result, $i, "post_name"); //combine blog url, with permalink title. Use this for title format
  41.  
  42. //format date
  43. $blog_date = strtotime($blog_date);
  44. $blog_date = strftime("%B %#d %Y", $blog_date);
  45.  
  46.  
  47. //the following HTML content will be generated on the page as many times as the loop runs. In this case 5.
  48. ?>
  49.  
  50.  
  51. <div class="post"></div>
  52.  
  53.    <br />
  54.         <span class="date">  <?php echo $blog_date; ?>:</span> | <?php echo '<a href="'. $blog_permalink .'">'; ?><?php echo $blog_title; ?></a><br />
  55.    <br />
  56.  
  57.         <?php echo $blog_content; ?> <br /><br />
  58.  
  59.  
  60. <?php
  61. } //end the for loop
  62. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement