WordPress obtains the most frequently viewed articles.

Source: Internet
Author: User
Tags php file

Statistics on article browsing times using pure code

1. The last one in the topic's functions. Php file?> Add the following code:

The code is as follows: Copy code
Function record_visitors ()
{  
If (is_singular ())
    {  
Global $ post;
$ Post_ID = $ post-> ID;
If ($ post_ID)
      {  
$ Post_views = (int) get_post_meta ($ post_ID, 'view', true );
If (! Update_post_meta ($ post_ID, 'view', ($ post_views + 1 )))
          {  
Add_post_meta ($ post_ID, 'Views ', 1, true );
          }  
      }  
    }  
}  
Add_action ('WP _ head', 'record _ visitors ');
/// Function name: post_views
/// Function purpose: obtain the number of times the document is read
Function post_views ($ before = '(click', $ after = ')', $ echo = 1)
{  
Global $ post;
$ Post_ID = $ post-> ID;
$ Views = (int) get_post_meta ($ post_ID, 'view', true );
If ($ echo) echo $ before, number_format ($ views), $ after;
Else return $ views;

2. Use the following code to call where you need to display the count:

Article read: <? Php post_views ('', 'sub');?>

Obtain the most viewed articles

In general, this is used to get popular articles. If you want to get the most frequently viewed articles from the above function, you can find the last article in the functions. Php file?> Add the following code:

The code is as follows: Copy code

/// Function purpose: get the most read articles
Function get_most_viewed_format ($ mode = '', $ limit = 10, $ show_date = 0, $ term_id = 0, $ beforetitle = '(', $ aftertitle = ')', $ beforedate = '(', $ afterdate = ')', $ beforecount = '(', $ aftercount = ')'){
Global $ wpdb, $ post;
$ Output = '';
$ Mode = ($ mode = '')? 'Post': $ mode;
$ Type_ SQL = ($ mode! = 'Both ')? "AND post_type = '$ mode '":'';
$ Term_ SQL = (is_array ($ term_id ))? "AND $ wpdb-> term_taxonomy.term_id IN (". join (',', $ term_id). ')': ($ term_id! = 0? "AND $ wpdb-> term_taxonomy.term_id = $ term_id ":'');
$ Term_ SQL. = $ term_id? "AND $ wpdb-> term_taxonomy.taxonomy! = 'Link _ category '":'';
$ Inr_join = $ term_id? "Inner join $ wpdb-> term_relationships ON ($ wpdb-> posts. ID = $ wpdb-> term_relationships.object_id) inner join $ wpdb-> term_taxonomy ON ($ wpdb-> term_relationships.term_taxonomy_id = $ wpdb-> login )":'';
// Database query
$ Most_viewed = $ wpdb-> get_results ("select id, post_date, post_title, (meta_value + 0) AS views FROM $ wpdb-> posts left join $ wpdb-> postmeta ON ($ wpdb-> posts. ID = $ wpdb-> postmeta. post_id) $ inr_join WHERE post_status = 'Publish 'AND post_password = ''$ term_ SQL $ type_ SQL AND meta_key = 'View' GROUP BY ID ORDER BY views DESC LIMIT $ limit ");
If ($ most_viewed ){
Foreach ($ most_viewed as $ viewed ){
$ Post_ID = $ viewed-> ID;
$ Post_views = number_format ($ viewed-> views );
$ Post_title = esc_attr ($ viewed-> post_title );
$ Get_permalink = esc_attr (get_permalink ($ post_ID ));
$ Output. = "<li> $ beforetitle $ post_title $ aftertitle ";
If ($ show_date ){
$ Posted = date (get_option ('date _ format'), strtotime ($ viewed-> post_date ));
$ Output. = "$ beforedate $ posted $ afterdate ";
    }  
$ Output. = "$ beforecount $ post_views $ aftercount </li> ";
   }  
} Else {
$ Output = "<li> N/A </li> n ";
  }  
Echo $ output;

Then use the following function call:

The code is as follows: Copy code

<? Php get_most_viewed_format () ;?>

Summary

What the tribe needs to add here is that the number of articles browsed by the above method is different from the WP-Postviews plug-in. The code here is obtained, in fact, even search engine crawlers will make statistics.

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.