I am in the process of writing some php to query my wordpress blog database and display the latest post on my homepage which is outside the wordpress environment.
I am not very well-versed in php but i have been able to display the latest blog title, as well as the post content. What I would like to do is have the thumbnail be a clickable link to the post. How do i get the link to the post? I also want to display just the excerpt not the entire post-however using post_excerpt in the same fashion as i have with post_title, post_content, does not seem to work.
// ...Connect to WP database
$dbc = mysql_connect(XXX,XXX,XXX);
if ( !$dbc ) {
die( 'Not Connected: ' . mysql_error());
}
// Select the database
$db = mysql_select_db('wrd_2ikhd5ho53');
if (!$db) {
echo "There is no database: " . $db;
}
// ...Formulate the query
$query = "
SELECT post_title,post_content,UNIX_TIMESTAMP(post_date) AS post_date_unix, ID
FROM `wp_posts`
WHERE `post_status` = 'publish'
AND `post_password` = ' '
AND `post_type` = 'post'
ORDER BY `wp_posts`.`post_date` DESC
";
// ...Perform the query
$result = mysql_query( $query );
// ...Check results of the query and terminate the script if invalid results
if ( !$result ) {
$message = '<p>Invalid query.</p>' . "n";
$message .= '<p>Whole query: ' . $query ."</p> n";
die ( $message );
}
// Init a variable for the number of rows of results
$num_rows = mysql_num_rows( $result );
$row = mysql_fetch_array( $result, MYSQL_ASSOC );
// Init var for DATE of the post
$post_date = date( "l, F jS, Y ", $row['post_date_unix'] );
// Init var for TITLE of the post
$post_title = $row['post_title'];
// Init var for CONTENT of the post
$post_content = $row['post_content'];
$post_excerpt = $row['post_excerpt'];
// Init var for Excerpt of the post
// Print the number of posts
echo "$post_title";
echo "$post_date";
// Free the resources associated with the result set
if ( $result ) {
mysql_free_result( $result );
mysql_close();
}
?>
the website to reference is http://www.uniconutrition.com
Thanks guys
It can be much easier than what you’re trying by using the WordPress database layer. See http://codex.wordpress.org/Integrating_WordPress_with_Your_Website
Basically:
Or, there is always RSS to grab one item, title and excerpt from WordPress.
Developer’s Guide – Google AJAX Feed API – Google Code
As far as the excerpt is concerned, you aren’t
SELECT
ing it in your query. Change the beginning of your query to:As for the link to the post, I’m not 100% sure you can get a “pretty-printable” link without going through the WordPress machinery. A short-link style URL is available under the
guid
column in thewp_posts
table, but the WP docs claim that it may not show up if pretty permalinks are disabled. Since you know the ID of the post (it’s a part of your query), you could just use the WordPress functions to get the link. See the get_permalink documentation page for more.As an aside, have a look at PDO or Mysqli as a replacement for the
mysql_*
functions. You should no longer use the latter.