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 ); |