开发者

How to make a plugin to count visitors for posts under specific category

How to开发者_StackOverflow中文版 count number of visitors for post under specific category ? Can I make such plugin who can do the whole magic ? I don't want plugin users to modify theme files or add code snippets in other theme files ...


something along the lines of adding to the post meta could do what you're wanting.

<?php    
add_action('the_content', 'myplugincb');

function myplugincb() {
    global $wp_query;
    if (count($wp_query->posts) == 1) { //just do this for individual posts/pages
    $pid = $wp_query->posts[0]->ID;
    $key = 'myplugin_post_visit_counter';
    update_post_meta($pid, $key, get_post_meta($pid, $key)+1);
    }
}

function myplugin_show_viewed($post_id) {
    return get_post_meta($post_id, 'myplugin_post_visit_counter');
}

You'd have to change that quite a few different ways depending on your desired result. You probably want to use something like Google Analytics if you're wanting to see specifics on pages visited and where the user came from etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜