Added the latest visitor feature to the WordPress blog page.

Source: Internet
Author: User
Tags wordpress blog


Native wp functions cannot be implemented, so they can only be used in the second place. Visitors who call recent messages will, of course, only display them once.

Put the following code in functions. php:

 

The code is as follows: Copy code
// Obtain the latest reader
Function visitors ($ tim, $ lim ){
Global $ wpdb;
$ Query = "select count (comment_ID) AS cnt, comment_author, comment_author_url, comment_author_email FROM (SELECT * FROM $ wpdb-> comments left outer join $ wpdb-> posts ON ($ wpdb-> posts. ID = $ wpdb-> comments. comment_post_ID) WHERE comment_date> date_sub (NOW (), INTERVAL $ tim day) AND comment_author_email! = ''AND post_password ='' AND comment_approved = '1' AND comment_type = '') AS tempcmt group by comment_author_email order by comment_date desc limit $ lim ";
$ Wall = $ wpdb-> get_results ($ query );
Foreach ($ wall as $ comment)
{
If ($ comment-> comment_author_url)
$ Url = $ comment-> comment_author_url;
Else $ url = "#";
$ R = "rel = 'external nofollow '";
$ Tmp = "<li> <a href = '". $ url. "'". $ r. "title = '". $ comment-> comment_author. "Left ". $ comment-> cnt. "Info> ". get_avatar ($ comment-> comment_author_email, 40 ). "</a> </li> ";
$ Output. = $ tmp;
}
Echo $ output;
}

The above is the core function, and then you need to make a small tool:

The code is as follows: Copy code


// Register the Widget
Add_action ('widgets _ init ', create_function ('', 'Return register_widget (" mk_visitors ");'));
Class mk_visitors extends WP_Widget {
// Register a WordPress gadget
Function mk_visitors (){
$ This-> WP_Widget ('MK _ visitors ', 'reader wall', array ('description' => 'Show the most recent comments '));
 }
// Front-end display gadgets
Function widget ($ args, $ instance ){
Extract ($ args, EXTR_SKIP );
Echo $ before_widget;
$ Title = apply_filters ('widget _ name', $ instance ['title']);
$ Limit = $ instance ['limit'];
$ Timer = $ instance ['timer'];
Echo $ before_title. $ title. $ after_title;
Echo '<ul class = "visitors"> ';
Echo visitors ($ tim = $ timer, $ lim = $ limit );
Echo '</ul> <div class = "clear"> </div> ';
Echo $ after_widget;
 }
// Save the gadgets setting options
Function update ($ new_instance, $ old_instance ){
$ Instance = $ old_instance;
$ Instance ['title'] = strip_tags ($ new_instance ['title']);
$ Instance ['limit'] = strip_tags ($ new_instance ['limit']);
$ Instance ['timer'] = strip_tags ($ new_instance ['timer']);
Return $ instance;
 }
// Form of background gadgets
Function form ($ instance ){
$ Instance = wp_parse_args (array) $ instance, array (
'Title' => 'Recent reader ',
'Limit' => '15 ',
'Timer' => '30'
   )
);
$ Title = strip_tags ($ instance ['title']);
$ Limit = strip_tags ($ instance ['limit']);
$ Timer = strip_tags ($ instance ['timer ']);
Echo '<p> <label> Title: <input class = "widefat" id = "'. $ this-/> get_field_id ('title '). '"name = "'. $ this-> get_field_name ('title '). '"type =" text "value = "'. $ instance ['title']. '"/> </label> </p> <label> Display quantity: <input class =" widefat "id = "'. $ this-/> get_field_id ('limit '). '"name = "'. $ this-> get_field_name ('limit '). '"type =" number "value = "'. $ instance ['limit']. '"/> </label> </p> <label> within a few days: <input class =" widefat "id = "'. $ this-/> get_field_id ('timer '). '"name = "'. $ this-> get_field_name ('timer '). '"type =" number "value = "'. $ instance ['timer']. '"/> </label> </p> ';
 }
}

The above description has been very careful, and then there is a style issue. This can be used freely. Below is the style of Mingkai blog:

The code is as follows: Copy code
. Sidebar ul {
List-style: none;
Margin: 1.5em 0;
Padding: 0;
}
. Sidebar ul li {
Margin: 0;
Padding: 5px 0;
Border-top: 1px solid # ddd;
Color: #969696;
}
. Visitors li {float: left ;}

In fact, most of them come with themes.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.