Part 5: How To Display Wordpress posts on your PHP site.

Posted by | Posted in Development, School, Tutorial, Uncategorized | Posted on 17-10-2008

Welcome to the Final Part 5: How To Display Wordpress posts on your PHP site

This is the last part of the tutorial. Here you can see the Final codes. and you can use the See the final result on my demo test page.

The final Codes:

I will now post the final codes of the index.php and the wpconf.php file. You may use the Codes in it but please link back to my blog if you use it. I would like that. Well here are the codes and i hope i helped you a bit with it.

Index.php:

<html>
<head>
<title>Test Page Title</title>
<?php require("wpconf.php"); ?>
</head>
<body>
<?php
 // keeps getting the next row until there are no more to get
 while($row = mysql_fetch_array( $result )) {   ?> 

<a href="<?php echo $row['guid']; ?>"><?php echo $row['post_title']; ?></a><br><br>
Post Date: <?php echo $row['post_date_gmt']; ?>
<hr> 

<?php
}
?>
</body>
</html>

wpconf.php

<?php

 // Make a MySQL Connection
mysql_connect("localhost", "DatabaseUser", "DatabasePassword") or die(mysql_error());
mysql_select_db("DatabaseName") or die(mysql_error());

// Get all the data from the "wp_posts" table
$result = mysql_query("SELECT * FROM wp_posts WHERE post_type='post' ORDER BY ID DESC LIMIT 15")
or die(mysql_error());

?>

Thank you for reading my tutorial and i hope you are happy with it. Take care and keep coding ;) Also keep designing for those who do that. 

The end of Part 5, And the end of the Tutorial.


Part 4: How To Display Wordpress posts on your PHP site.

Posted by | Posted in Development, School, Tutorial, Uncategorized | Posted on 17-10-2008

Welcome to Part 4: How To Display Wordpress posts on your PHP site

I will continue where i stoped at Part 3. So if you did just come onto this page and you didn’t read Part 3 Go to the bottom of this post and click on Part 3.

Get info in a Template:

Now the database send all information to the wpconf.php and the index.php gets all info from the wpconf.php file we need to put the right php code into the index.php file. We do that by using these kind of codes:

<?php echo $row['rowname']; ?>

Which codes we need:

in Part 1 you have thought about which information you want on your php page. Now we know how to get it there we need to create that. We do that by the previous code, we replace the “rowname” with the name of the info you want to be added at that place. And example is the “post_title”:

<?php echo $row['post_title']; ?>

Getting the Row info till it’s empty:

Before we add the previous codes to the template we will need to specify which part of the template php needs to repeat till the MySQL database has no entry’s anymore. We do that with the following code:

<?php
 // keeps getting the next row until there are no more to get
 while($row = mysql_fetch_array( $result )) {	?>

//The Content//

<?php
}
?>

The content between it can be replaced by a div or many Div’s. It can have a total Post entry template in it. We will keep it simple and we will only add a <hr> between the entry’s (a horizontal line). So now the index.php will look like:

<html>
<head>
<title>Test Page Title</title>
<?php require("wpconf.php"); ?>
</head>
<body>
<?php
 // keeps getting the next row until there are no more to get
 while($row = mysql_fetch_array( $result )) {	?>

//The Content//
<hr>

<?php
}
?>
</body>
</html>

Adding the Row codes:

Now we have specified which part of the code PHP needs to repeat till it’s empty we will add the Info into it. In this way php will display the info Per post. So the exact link to the post the author from that post, etc.. We can now add all the info into it. For example:

<body>
<?php
 // keeps getting the next row until there are no more to get
 while($row = mysql_fetch_array( $result )) {	?>

<a href="<?php echo $row['guid']; ?>"><?php echo $row['post_title']; ?></a><br><br>
Post Date: <?php echo $row['post_date_gmt']; ?>
<hr>

<?php
}
?>
</body>

Offcourse you can add multiple other Rows. I showed only and example of it. Just try it out and you will see that it’s very easy to create.

My working example is in the demo directory at:
Demo php page: http://www.marceldoornbos.com/demo/site/index.php
Demo Blog: http://www.marceldoornbos.com/demo/blog
(For the blog i used Wordpress 2.6.2, The latest version at this moment)

You can see the result there.

The Final 5th part is the Full Code of the index.php and wpconf.php File.
so if you want ot see the full code go to part 5.

The end of Part 4


Part 3: How To Display Wordpress posts on your PHP site.

Posted by | Posted in Development, School, Tutorial, Uncategorized | Posted on 17-10-2008

Welcome to Part 3: How To Display Wordpress posts on your PHP site

I will continue where i stoped at Part 2. So if you did just come onto this page and you didn’t read Part 2 Go to the bottom of this post and click on Part 2.

Ordering The MySQL Entrys:

In the previous part we added the MySQL Query to get the posts from one specific Table. But when we keep the codes like this we will get all posts out of the Table which includes pages and Draft posts. (The automaticly saved posts) So we need to Sort the posts. We will sort it so we only get the posts which are Published. We Do that with the following Code:

WHERE post_type='post'

Sorting at Correct Order:

We also want the Posts to be Displayed as the Latest First. Not the first post at the top. So we order all posts in a Descending order. When you do Ascending you will have the first post on top and the last post at the bottom. You can also choose for “rand()” Which is Random. But that is not really good since you get all posts
not in any post order. So it can be post:  2 than 6 than 3 than 1, etc.. We will do a Descending order by Post ID (For a correct order) by adding the following code right after the previous code:

ORDER BY ID DESC

 Limit the posts Per Page:
 
When we have over 100 posts we don’t want all posts to be Displayed on the Latest Posts page 
we create. So we will limit the count of entrys that will show. By adding a Limit code to the previous code. and adding the count of posts that should be displayed. We will do that by adding the following code(15 is the number of posts):

LIMIT 15

Now the wpconf.php should look like this:

<?php

 // Make a MySQL Connection
mysql_connect("localhost", "DatabaseUser", "DatabasePass") or die(mysql_error());
mysql_select_db("DatabaseName") or die(mysql_error());

// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM wp_posts WHERE post_type='post' ORDER BY ID DESC LIMIT 15")
or die(mysql_error());

?>

In the next part we will explain how we get all the info in the index.php test page.
so please click on the right part below to go to part 4. 

The end of Part 3


Part 2: How To Display Wordpress posts on your PHP site

Posted by | Posted in Development, School, Tutorial, Uncategorized | Posted on 16-10-2008

Welcome to Part 2: How To Display Wordpress posts on your PHP site

I will continue where i stoped at Part 1. So if you did just come onto this page and you didn’t read Part 1 Go to the bottom of this post and click on Part 1.

Creating a Test PHP page:

Now we know what information we want to display on the php site, we will continue by coding it into the Page. (I use a Test php page to show you how it works.) 

My working example is in the demo directory at:
Demo php page: http://www.marceldoornbos.com/demo/site/index.php
Demo Blog: http://www.marceldoornbos.com/demo/blog
(For the blog i used Wordpress 2.6.2, The latest version at this moment)

Creating the index.php file:

Now we are going to create the testpage index.php file. If you are creating a Testpage also(Which i recommend) Follow my Steps. Otherwise you will allready have the main body, etc..

We start with the basic Index.php:
 

<html>
<head>
<title>Test Page Title</title>
</head>
<body>
</body>
</html>

Adding MySQL Info:

So now we have created the basic Index.php We will create another file called wpconf.php and add The MySQL Information to the wpconf.php file which is necessary to login to the MySQL Database. It uses the DB User, DB name, and Database Password. This info is the same as the Database info from your Wordpress blog. So now we will Add this to the wpconf.php:

<?php

// Make a MySQL Connection
mysql_connect("localhost", "DatabaseUser", "DatabasePassword") or die(mysql_error());
mysql_select_db("DatabaseName") or die(mysql_error()); 

?>
 

And wel will add the following line of code to the index.php between the and tags.:

<?php require("wpconf.php"); ?>

So now the index.php will look like:

<html>
<head>
<title>Test Page Title</title>
<?php require("wpconf.php"); ?>
</head>
<body>
</body>
</html>

This basicly Connects the wpconf.php to the database from the wordpress blog. and the wpconf.php sends the information to the index.php file. IF the mysql is incorrect it will Quit the connection and than the script won’t get any information from the Database, “or die(mysql_error()” checks if the information was correct and otherwise it would quite the connection. 

Getting Data Only from the wp_posts Table:

Now we will add a Database Query to the wpconf.php file. We create a Variable for the query so we can easily call it from the actual php code. Let’s call that variable “$result”. Lets add the following Code to the document: 

// Get all the data from the "wp_posts" table
$result = mysql_query("SELECT * FROM wp_posts")
or die(mysql_error());

This code basicly sellects only the table wp_posts. We do that because we only need to information from that table because that is used at the frontpage of the blog. also again there is a “or die(mysql_error());” So if there is no table called “wp_posts” the mysql connection will be canceled and the script won’t get any information. 

Now the wpconf.php should look like this:

<?php

// Make a MySQL Connection
mysql_connect("localhost", "DatabaseUser", "DatabasePassword") or die(mysql_error());
mysql_select_db("DatabaseName") or die(mysql_error()); 

// Get all the data from the "wp_posts" table
$result = mysql_query("SELECT * FROM wp_posts")
or die(mysql_error());
?>

In the next part we will further get into the Order and Sorting of a MySQL database using the code above. so please click on the right part below to go to part 3. 

The end of Part 2


Part 1: How To Display Wordpress posts on your PHP site.

Posted by | Posted in Development, School, Tutorial | Posted on 16-10-2008

Hi, 

Introduction:

A Few days ago i was searching the web on a tutorial on how to display wordpress posts Outside the wordpress directory. for example:     Wordpress is installed at: http://www.yourdomain.com/wordpress  and you want to display the wordpress posts in the members area at: http://www.yourdomain.com/members/index.php  in a news block or something. 

Since i didn’t find anything i thought about ways of doing this. The best thing that came up into my mind was using the MySQL database from wordpress itself. I’ve been paying with the code and i found out how to do that. And now i will explain everything to you so you can do the same when you are creating new websites.

What do you need: 

You will need:

- A Wordpress Blog (Where the posts must come from)
- The Database information.
- Another Website where you want to Display the posts.
- a Good Text Editor.(Notepad++ is Recommended)
- Knowledge of HTML and PHP and maybe some CSS.

What info do you want:

First of all you need to think about which info you will need from the posts. There are multiple fields of into in the Wordpress Database like: the title, the author, the link, the Post Date, the Post Date GMT, the modification date, the modification date GMT, the comment count, the content, the post type, Etc…

There is many info so i suggest you to write down the options you want.

phpMyAdmin overview: 

Login to phpMyAdmin and go to your Wordpress Database. There you should see the following:

wp_posts Table overview:

Out of the list above look for wp_posts and click on the first action button. Than you will see how it is build And what is inside the different rows. (It’s much easier to see which info is stored under which Row name when you use that action.) After you’ve clicked that you will see something similair to this: 

There you see See the above, with alot more rows with info in it. All those info represents the Wordpress Postmeta which is displayed on the frontpage of the Wordpress Blog. In that way you can fully recreate the index page of the wordpress blog. Or you can fully Integrate it into an excisting CMS. 

The end of Part 1