Fetching wordpress posts and placing them in your html page can be done easily!

  You are currently not logged in. You can view the forums, but cannot post messages. Log In | Register

09-Dec-09 11:20

<?php
//db parameters
$db_username = 'username';
$db_password = 'password';
$db_database = 'databasename';

$blog_url = 'http://www.elated.com/blog/'; //base folder for the blog

//connect to the database
mysql_connect(localhost, $db_username, $db_password);
@mysql_select_db($db_database) or die("Unable to select database");

//get data from database
$query = "Select * FROM wp_posts WHERE post_type='post' AND post_status='publish' ORDER BY id DESC LIMIT 1";

$query_result = mysql_query($query);
$num = mysql_numrows($query_result);

//close database connection
mysql_close();

//assigning data to variables
$blog_date = mysql_result($query_result, 0, "post_date");
$blog_title = mysql_result($query_result, 0, "post_title");
$blog_content = mysql_result($query_result, 0, "post_content");
//$blog_permalink = mysql_result($query_result, 0, "guid"); //use this for 'p=11' format

$blog_permalink = $blog_url . mysql_result($query_result, 0, "post_name"); //combine blog url, with permalink title. Use this for title format

//date
$blog_date = strtotime($blog_date);
$blog_date = strftime("%b %e", $blog_date);

$blog_content = strip_tags($blog_content);

?>
< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
</head>


and now, place the following code in the div, you want to display!!




<div>

<h2><?php echo $blog_date; ?>:</h2>

<i><?php echo $blog_title; ?> </i>

<p><?php echo $blog_content; ?></p>

</div>


[Edited by matt on 10-Dec-09 20:45]

--
knowledge is to share.. not to compare!!
www.c4di.org.uk
www.elated.com
10-Dec-09 20:46
Thanks for the tip desibird! I've added code tags to your code to make it easier to read, and removed that stray ?>...

Cheers,
Matt

--
Matt Doyle, Elated
17-Dec-09 16:08
Came across this method, which was simple to use.
www.corvidworks.com/articles/wordpress-content-on-other-pages
18-Dec-09 04:17
@spiderling: the one within the corvidworks site is all about turning your whole site into/under wordpress influence.

this present topic is about just, sucking the posts directly from database and posting them into desired content area.

We can even use this technique to many of cms's that are using same structure of data storing!

--
knowledge is to share.. not to compare!!
www.c4di.org.uk
www.elated.com
18-Dec-09 22:03
@desibird: I don't think the corvidworks approach is about making your whole site use WordPress. It's merely pulling in the WordPress library file wp-blog-header.php in order to display the posts. In fact this is probably a better approach since it'll still work if the database details change at a later date (username, password, database name or, for that matter, the WordPress table schema).

Cheers,
Matt

--
Matt Doyle, Elated

 
New posts
Old posts

Follow Elated