Jamie
June 12th, 2007, 14:00
I'm using some PHP which outputs the latest post from a database.
I want to display the number of comments (field name "comment_count" inside the "wp_posts" table) for the latest post within the "wp_posts" table which has a value of "1" inside the "post_category" field. Any post with a different value inside "post_category" should be ignored.
Here is the code I'm using which brings in the latest post (including number of comments) by looking at all categories.
<?php
$how_many=1; //How many posts do you want to show
require_once("http://www.mydomain.com/wp-config.php"); // Change this for your path to wp-config.php file
?>
<?
$news=$wpdb->get_results("SELECT ID,post_title, post_content, post_date, post_name, comment_count FROM wp_posts WHERE post_status = 'publish' and post_type = 'post' ORDER BY ID DESC LIMIT ".$how_many);
foreach($news as $np) {
$d = strtotime($np->post_date);
$cc = $np->comment_count;
$link_text = ($cc == 1) ? "$cc comment" : "$cc comments";
echo "<p class=\"smalltext\">" . date('M d, Y', strtotime($np->post_date) . "");
echo "<br /><a href='http://www.jamieharrop.com/" . date('Y/m/d',$d) . "/" . $np->post_name . "/#comments'>" . $link_text . "</a></p>\n";
echo "<p>" . $np->post_content . "<br />";
}
?>
I have stripped it down to it's most basic form (I removed some of the echo's that were irrelavant).
Thanks in advance.
I want to display the number of comments (field name "comment_count" inside the "wp_posts" table) for the latest post within the "wp_posts" table which has a value of "1" inside the "post_category" field. Any post with a different value inside "post_category" should be ignored.
Here is the code I'm using which brings in the latest post (including number of comments) by looking at all categories.
<?php
$how_many=1; //How many posts do you want to show
require_once("http://www.mydomain.com/wp-config.php"); // Change this for your path to wp-config.php file
?>
<?
$news=$wpdb->get_results("SELECT ID,post_title, post_content, post_date, post_name, comment_count FROM wp_posts WHERE post_status = 'publish' and post_type = 'post' ORDER BY ID DESC LIMIT ".$how_many);
foreach($news as $np) {
$d = strtotime($np->post_date);
$cc = $np->comment_count;
$link_text = ($cc == 1) ? "$cc comment" : "$cc comments";
echo "<p class=\"smalltext\">" . date('M d, Y', strtotime($np->post_date) . "");
echo "<br /><a href='http://www.jamieharrop.com/" . date('Y/m/d',$d) . "/" . $np->post_name . "/#comments'>" . $link_text . "</a></p>\n";
echo "<p>" . $np->post_content . "<br />";
}
?>
I have stripped it down to it's most basic form (I removed some of the echo's that were irrelavant).
Thanks in advance.