If you’ve been wondering how to do this and have tried the the_excerpt() function but it wasn’t what you were quite looking for then fear not!  Bluepoint SEO has the PHP & MySQL code here along with instructions on using it so you can get a customised excerpt onto your homepage, or in fact on any page on your website.

What’s so good about having blog post snippets on your homepage?

Well from a usability point of view it allows anyone visiting your website to travel straight to your blog post.  If they see the snippet and the title and the first sentence grabs their attention then you have user engagement on your site.

From an SEO point of view it keeps your homepage updated with new content so Google and Bing will frequent your site more often to check for updated articles.  Not just this, but your homepage is typically the most linked to page on your site (not always but usually from our experience) and this means it has greater authority than other pages.  A link from the homepage to your new blog post will pass on this authority and give your blog post a greater chance of being found by people searching for your article’s topic in the search engine result pages.

This blog snippet code will return the 5 newest blog posts displaying the title of the post and the first 45 characters of the first sentence.  If you study the MySQL statement you’ll notice you can actually obtain the following information:

  • Title of blog post
  • Contents of the post (limited by 45 characters)
  • Post date
  • URL of the actual post for the href attribute

If you want to use any of the other data listed above returned from the WordPress posts_table then you can reference it through:

<?php

$someValue = $row_[‘corresponding_column_name’];

?>

Okay the PHP code and it’s super easy to implement/copy & paste:

<?php
// Snippet 0.2 Created (2014)

$db = ‘DATABASE HERE’;
$user = ‘DB USER NAME HERE$’;
$pass = ‘DATABASE PASSWORD’;
$host = ‘HOST OF SQL SERVER’;
$row = array();
$q = “SELECT post_title, post_content, post_date, guid FROM posts_table WHERE post_status=’publish’ ORDER BY post_date DESC LIMIT 0, 5”;
$date = array();
$errors = array();

$handle = mysqli_connect($host, $user, $pass, $db);

if ( !isset($handle) ) {
$errors = ‘Could not connect to database.’;
}
else {

$result = mysqli_query($handle, $q);
echo “<ul id=\”snippet\”>”;
while ( $row = mysqli_fetch_array($result) ) {
echo ‘<li><a href=”‘.$row[‘guid’].’”>’.trim(substr(strip_tags($row[‘post_title’].’: ‘.$row[‘post_content’]), 0, 45)).’…</a></li>’;
}
echo “</ul> <!– eof snippet –>”;
}

while ( $errors != NULL ) {
echo $errors . ‘</br >’;
}

die();
?>

How do I get the snippet on my homepage then?

Copy and paste the above PHP code into your website where you would like it to appear.  Alternatively, you could put it in an include.inc.php file and include it wherever you need it to appear on your web pages with the PHP code like this:

<?php include(‘/path/to/include/file/include.inc.php’); ?>

Don’t forget to alter the details in capital letters at the top of the code to suit your MySQL server.  This script won’t work without your own specific server credentials.

I hope you find this PHP snippet useful and if you have any questions feel free to e-mail the Bluepoint SEO team and we’d be happy to help.

Happy coding!