WordPress – Posts As News Clips Elsewhere

Disclaimer

The instructions/steps given below worked for me running CentOS. It may very well work for you on other linux distributions, Red Hat-like or otherwise. Please note that if you decide to use these instructions on your machine, you are doing so entirely at your very own discretion and that neither this site, sgowtham.com, nor its author is responsible for any/all damage – intellectual or otherwise.

What Is The Need?

More often than not, a WordPress powered blog is (a big/small) part of a website and often times, it becomes necessary to display post-excerpts (from a diary or a journal, etc.) in the main page. This can often help visitors/readers get a glimpse of what is on offer (like a menu at a restaurant) and decide if they want to actually read it.

How To Get It Done?

Let us suppose that the website has a main page, called index.php (URL: http://www.your-site.com/index.php), and that the WordPress powered blog is served from wordpress (URL: http://www.your-site.com/wordpress/) folder. Let us suppose that we need to display the excerpts of the latest 5 posts in index.php. To that effect, let us create a file – wp_latest_posts.php – with following contents:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<!--?php 
 
# wp_latest_posts.php
# Displays a pre-set number of post-excerpts in a page
# outside of wordpress folder.
# This script must reside in the same folder as of index.php
# (http://www.your-site.com/index.php)

# MySQL connection details (refer to wp-config.php)
$host          = "localhost";
$dbuser        = "WORDPRESS_USER";
$dbpasswd      = "WORDPRESS_PASSWORD";
$database      = "WORDPRESS_DATABASE";
$table_prefix  = 'wp_';
$table_posts   = $table_prefix . 'posts';
 
# Connect to MySQL
$connect = mysql_connect($host, $dbuser, $dbpasswd) or die(mysql_error());
mysql_select_db($database,$connect) or die(mysql_error());
 
# Number of post-excerpts to be displayed on index.php
$num_latest = 5; 
 
# Perform the query
# This selects everything from '$table_posts' table which are 
# PUBLISHED and NOT PAGES, and reverse orders them by POST DATE.
$sql       = "SELECT * FROM $table_posts WHERE post_status LIKE ";
$sql      .= "\"%publish%\" AND post_type != 'page' ";
$sql      .= "ORDER BY post_date DESC LIMIT $num_latest ";
$result    = mysql_query($sql) or
             die('Invalid Query:' . mysql_errno() . mysql_error());
$nresults  = mysql_num_rows($result);
 
# Display results
# The results are displayed as an un-ordered list. Can be modified
# to meet specific requirements with basic knowledge of HTML
print "&lt;/p&gt;
&lt;h3&gt;News Clips&lt;/h3&gt;
&lt;p&gt;\n";
print "&lt;/p&gt;
&lt;ul&gt;\n";
 
while ($post_details = mysql_fetch_array($result)) {
 
  # Store the values in varaibles 
  $post_guid    = $post_details['guid'];
  $post_title   = $post_details['post_title'];
  $post_date    = $post_details['post_date'];
  $post_content = $post_details['post_content'];
 
  # Convert Date in to 'July 1, 2008' format
  # Refer to PHP's date manual for more options
  $post_date   = strtotime($post_date);
  $post_date   = date('F j, Y', $post_date);
 
  # Generate post-excerpt (say the first 150 characters
  # of post-content)
  $post_content = strip_tags($post_content, '&lt;br&gt; &lt;sub&gt; &lt;/sub&gt;');
 
  if (strlen($post_content) &gt; 150 ){
    $post_excerpt = mb_substr($post_content, 0, 147) . '... [More]';
  } else {
    $post_excerpt = $post_content;
  }
 
  # Display the entry details
  # One can decorate these with a basic working knowledge of CSS
  print "&lt;/p&gt;
&lt;li&gt;
&lt;br ?-->

$post_title>
$post_date
$post_excerpt

\n”; } print ”

\n\n”; # Free up resources and close the connection to the database mysql_free_result($result); mysql_close($connect); ?>


To include this wp_latest_posts.php in index.php, one may use the following syntax at an appropriate place:

1
include('wp_latest_posts.php');

Screenshot

When everything goes smoothly, the result may look as follows:


wp_latest_posts

One Reply to “WordPress – Posts As News Clips Elsewhere”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.