All Good Things Must Come To An End

As you all know, I have been doing WordPress plugins and supporting it for the past 6 years. These 6 years of my life, I have been through my polytechnic education, my national service as well as my university education.

I just graduated from university in December 2009 and have been looking for full-time jobs. I am offered a full-time job and will be starting work on 1st February 2010.

I regret to say that I am NOT ABLE to provide support for my plugins anymore due to my full-time job commitment. I will leave this forum open and let the community help one another.

However, I WILL still update my plugins whenever I can and you still can report bugs to me via email and I will try to fix it.

Author Topic: WordPress Recent posts and Comments Widget PHP function  (Read 11430 times)

0 Members and 2 Guests are viewing this topic.

Offline Romik84

  • Jr. Member
  • **
  • Posts: 99
    • View Profile
Hi folks, I came with another problem. There are many plugins to get recent posts and comments but I would like to use function of the built-in widgets. Here are codes.

Recent posts:

Code: [Select]
function wp_widget_recent_entries($args) {
if ( '%BEG_OF_TITLE%' != $args['before_title'] ) {
if ( $output = wp_cache_get('widget_recent_entries', 'widget') )
return print($output);
ob_start();
}

extract($args);
$options = get_option('widget_recent_entries');
$title = empty($options['title']) ? __('Recent Posts') : $options['title'];
if ( !$number = (int) $options['number'] )
$number = 10;
else if ( $number < 1 )
$number = 1;
else if ( $number > 15 )
$number = 15;

$r = new WP_Query("showposts=$number&what_to_show=posts&nopaging=0&post_status=publish");
if ($r->have_posts()) :
?>
<?php echo $before_widget?>
<?php echo $before_title $title $after_title?>
<ul>
<?php  while ($r->have_posts()) : $r->the_post(); ?>
<li><a href="<?php the_permalink() ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?> </a></li>
<?php endwhile; ?>
</ul>
<?php echo $after_widget?>
<?php
wp_reset_query();  // Restore global post data stomped by the_post().
endif;

if ( '%BEG_OF_TITLE%' != $args['before_title'] )
wp_cache_add('widget_recent_entries'ob_get_flush(), 'widget');
}

Recent comments
Code: [Select]
function wp_widget_recent_comments($args) {
global $wpdb, $comments, $comment;
extract($args, EXTR_SKIP);
$options = get_option('widget_recent_comments');
$title = empty($options['title']) ? __('Recent Comments') : $options['title'];
if ( !$number = (int) $options['number'] )
$number = 5;
else if ( $number < 1 )
$number = 1;
else if ( $number > 15 )
$number = 15;

if ( !$comments = wp_cache_get( 'recent_comments', 'widget' ) ) {
$comments = $wpdb->get_results("SELECT comment_author, comment_author_url, comment_ID, comment_post_ID FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT $number");
wp_cache_add( 'recent_comments', $comments, 'widget' );
}

Will u help me how to get php function to show 4 entries with dates? Thank u. :)


Offline GaMerZ

  • lesterchan.net
  • Administrator
  • *****
  • Posts: 11,692
    • View Profile
  • WordPress Version: WordPress 3.0.x
  • Theme Name: lesterchan.net v3.4
Re: WordPress Recent posts and Comments Widget PHP function
« Reply #1 on: 27 May 2008, 15:53 »
sure sure,
Code: [Select]
wp_widget_recent_comments("number=4") I don't think there is an option for showing the date

++ lesterchan.net - Lester Chan's Website

I regret to say that I am not able to provide support for my plugins anymore due to my full-time job commitment. I will leave this forum open and let the community help one another.

Offline Romik84

  • Jr. Member
  • **
  • Posts: 99
    • View Profile
Re: WordPress Recent posts and Comments Widget PHP function
« Reply #2 on: 27 May 2008, 19:05 »
I have already tried the code before Lester, but I got an error
Code: [Select]
Warning: extract() [function.extract]: First argument should be an array in
Well, the first problem "Recent posts" I have already solved. I found this code on the net:

Code: [Select]
<?php $my_query = new WP_Query('showposts=4');
while (
$my_query->have_posts()) : $my_query->the_post(); ?>


<?php
if (strlen(the_title('','',FALSE)) > 80) {
$title_short substr(the_title('','',FALSE), 080);
preg_match('/^(.*)\s/s'$title_short$matches);
if (
$matches[1]) $title_short $matches[1];
$title_short $title_short.'...';
}
else
{
$title_short the_title('','',FALSE);
}
?>


<ul>
<li><span style="float:right;margin-left:10px;"><small><?php the_time('D, F jS'?></small></span> <a title="<?php echo the_title() ?>" href="<?php the_permalink() ?>"><?php echo $title_short ?></a></li>
</ul>
<?php endwhile; ?>

and it works nice and even with title excerpt and many features.

So, now my problem is the recent comments function. Any idea? thanks for each help :)

Offline GaMerZ

  • lesterchan.net
  • Administrator
  • *****
  • Posts: 11,692
    • View Profile
  • WordPress Version: WordPress 3.0.x
  • Theme Name: lesterchan.net v3.4
Re: WordPress Recent posts and Comments Widget PHP function
« Reply #3 on: 28 May 2008, 06:48 »
Take a look at my WP-Stats, I think I have a recent comments function inside.

++ lesterchan.net - Lester Chan's Website

I regret to say that I am not able to provide support for my plugins anymore due to my full-time job commitment. I will leave this forum open and let the community help one another.

Offline Romik84

  • Jr. Member
  • **
  • Posts: 99
    • View Profile
Re: WordPress Recent posts and Comments Widget PHP function
« Reply #4 on: 28 May 2008, 07:35 »
thanks, but it is the same function which I can use for the widget also? I would like to use the widget function if possible...

Offline GaMerZ

  • lesterchan.net
  • Administrator
  • *****
  • Posts: 11,692
    • View Profile
  • WordPress Version: WordPress 3.0.x
  • Theme Name: lesterchan.net v3.4
Re: WordPress Recent posts and Comments Widget PHP function
« Reply #5 on: 28 May 2008, 07:52 »
I think you can use a plugin that can execute php code within the widget and then add the code in.

++ lesterchan.net - Lester Chan's Website

I regret to say that I am not able to provide support for my plugins anymore due to my full-time job commitment. I will leave this forum open and let the community help one another.