Show only the last 5 posts of a certain category.
-
Hi,
I’ve been trying to solve this problem for ages now :).
In my side bar I need to show the last 5 columns (only the date/time and titel/link).
If you take a look at https://frontpage.klifix.nl/ you can see what I mean. In the left sidebar I have already the last 15 posts with time/date and nr of comments in a list.
In the exactly same way I want this just for 1 specific category called “columns” (cat id=30).This is what I use now for the normal last 15 posts:
<?php
$today = current_time('mysql', 1);function truncate($string,$chars,$append = '...') {
if(strlen($string) > $chars) {
$string = substr($string, 0, $chars);
$trunc_at = strrpos($string, ' ');if($trunc_at !== FALSE)
$string = substr($string, 0, $trunc_at);
$string = $string.$append;
}return $string;
}if ( $recentposts = $wpdb->get_results("SELECT ID, post_title, post_date FROM dotenel_posts WHERE post_status = 'publish' AND post_date_gmt < '$today' ORDER BY post_date DESC LIMIT 15")):
?><?php
foreach ($recentposts as $post) {
if ($post->post_title == '')
$post->post_title = sprintf(__('Post #%s'), $post->ID);$posttime = strtotime($post->post_date);
$mytime = date('<font color="red">H:i</font>', $posttime);
$mydate = date('<font color="red">d/m</font>', $posttime);
$currtime = time();
$today = date('d',$currtime);
$postday = date('d',$posttime);
if($postday==$today)
echo $mytime;
else
echo $mydate;echo " ID'>";
$shorten_title = truncate($post->post_title, 39);
echo $shorten_title;
echo '';$numcomments = $wpdb->get_var("SELECT COUNT(*) FROM dotenel_comments WHERE comment_post_ID = '{$post->ID}' AND comment_approved = '1'");
echo " ({$numcomments})
";}
?>
<?php endif; ?>What do I need to adjust on this code to show only the last 15 posts within 1 specific category? PLEASE HELP!
- The topic ‘Show only the last 5 posts of a certain category.’ is closed to new replies.