2
votes

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

2

2 Answers

6
votes

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:

<?php
require('/the/path/to/your/wp-blog-header.php');
?>

<?php
$posts = get_posts('numberposts=1');
foreach ($posts as $post) : start_wp(); ?>
<?php the_date(); echo "<br />"; ?>
<?php the_title(); ?>    
<?php the_excerpt(); ?> 
<?php
endforeach;
?>

Or, there is always RSS to grab one item, title and excerpt from WordPress.

Developer's Guide - Google AJAX Feed API - Google Code

<html>
  <head>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">

    google.load("feeds", "1");

    function initialize() {
      var feed = new google.feeds.Feed("http://fastpshb.appspot.com/feed/1/fastpshb");
      feed.load(function(result) {
        if (!result.error) {
          var container = document.getElementById("feed");
          for (var i = 0; i < result.feed.entries.length; i++) {
            var entry = result.feed.entries[i];
            var div = document.createElement("div");
            div.appendChild(document.createTextNode(entry.title));
            container.appendChild(div);
          }
        }
      });
    }
    google.setOnLoadCallback(initialize);

    </script>
  </head>
  <body>
    <div id="feed"></div>
  </body>
</html>
0
votes

As far as the excerpt is concerned, you aren't SELECTing it in your query. Change the beginning of your query to:

SELECT post_title, post_content, post_excerpt,
       UNIX_TIMESTAMP(post_date) AS post_date_unix, ID

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 the wp_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.