Wordpress,cos-html-cache the solution of the click-count failure problem after static

Source: Internet
Author: User
Tags cos

Installed WordPress cos-html-cache static plug-in, generated a static file, Post-views and so on, the number of clicks plug-in is invalid,

Find some, including a JS version, need to use the Post-views plug-in, I do not want to install too many plug-ins, so the following scenario.

1:advanced Custom fields plugin.

After installation, I used to add a new field as a thumbnail field: Hits

2: Send the article,

You can fill it out or leave it blank.
3: Modify the article template file

4:

wp_conter.php file

5: Effect
corresponding See figure
?

1. [Image]Wp-custom_fields.png 2. [Image]Wp-article_write.png 3. [Image]Wp-theme_js.png 4. [Image]Wp_counter.png 5. [Image]Wp_clicks2.png

? 6. Code [PHP] Code
<?php
/*
Plugin Name: Click to count Ajax statistics
version:1.0 Alternative Pictures
Author:zhy
Author Uri:http://blog.sina.com.cn/shengqingjingxin
*/http://www.enterdesk.com/special/linglei/?

Include_once (' wp-config.php ');
Include_once (' wp-includes/wp-db.php ');

Global $wpdb;
$postid = $_get[' id '];
if (Is_numeric ($postid)!=true) {
Exit ();
}

$strsql = "Select meta_value,post_id from $wpdb->postmeta where Meta_key = ' article_clicks ' and post_id= '. $postid;
$clicks = $wpdb->get_var ($strsql);
if ($clicks! = ") {
$clicks = $clicks +1;
$strout = $clicks;
$strsql = "UPDATE Wp_postmeta set meta_value=". $clicks. "Where Meta_key = ' article_clicks ' and post_id= '". $postid. " ";
$wpdb->query ($strsql);
} else {
$strout = "1";
$strsql = "INSERT into Wp_postmeta (post_id,meta_key,meta_value) VALUES (". $postid. ", ' article_clicks ', ' 1 ')";
$wpdb->query ($strsql);
}
echo $clicks +1;

?>
7. [Code][php] Code

Click: &nbsp;<span id= ' views ' ></span>&nbsp; times
<script>
$.get ("/wp-counter.php",
{
ID: "<?php echo get_the_id ();?>",
},
function (data,status) {
if (status = = = ' Success ') {
$ ("#views"). Text (data);
}else{
$ ("#views"). Text (<?php echo get_the_id ();?>);
}
});
</script>

Wordpress,cos-html-cache the solution of the click-count failure problem after static

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.