Wordpress php code version article views

Source: Internet
Author: User


I use the "statistics and output views" function, so I use the main function section.

 

The code is as follows: Copy code
Function custom_the_views ($ post_id, $ echo = true, $ unit = 'View '){
$ Count_key = 'views ';
$ Views = get_post_custom ($ post_id );
$ Views = intval ($ views ['View'] [0]);
If ($ views = ''){
Return '';
} Else {
If ($ echo ){
Echo number_format_i18n ($ views). $ unit;
} Else {
Return number_format_i18n ($ views). $ unit;
        }
    }
}
Function set_post_views (){
Global $ post;
$ Post_id = intval ($ post-> ID );
$ Count_key = 'views ';
$ Views = get_post_custom ($ post_id );
$ Views = intval ($ views ['View'] [0]);
If (is_single () | is_page ()){
If (! Update_post_meta ($ post_id, 'view', ($ views + 1 ))){
Add_post_meta ($ post_id, 'Views ', 1, true );
        }
    }
}
Add_action ('Get _ head', 'set _ post_views ');

Parameter description:

1. $ post_id: Document ID. Generally, this parameter is set to $ post-> ID.
2. $ echo: true/false, Display/get the number of views (default: Show-true)
3. $ views: Unit of views (default value: Space + views)

General call method:

The code is as follows: Copy code
<? Php if (function_exists ('m M _ the_views ') custom_the_views ($ post-> ID);?>

PS:
If the WP-Postviews plug-in is used

The code is as follows: Copy code

<? Php the_views ();?>

Change

<? Php custom_the_views ($ post-> ID);?>

You can.
Or:

The code is as follows: Copy code

<? Php if (function_exists (the _ views) the_views ();?>

Change

<? Php if (function_exists ('m M _ the_views ') custom_the_views ($ post-> ID );

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.