如何制作插件以计算特定类别下的帖子的访问者

时间:2011-04-27 12:03:57

标签: wordpress plugins counter

如何计算特定类别下的帖子数量?我能制作出能够完成所有魔法的插件吗?我不希望插件用户修改主题文件或在其他主题文件中添加代码片段...

1 个答案:

答案 0 :(得分:0)

添加到帖子元的内容可以做你想要的事情。

<?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');
}

根据您的预期结果,您必须改变这几种不同的方式。如果您想要查看访问过的网页上的细节以及用户来自哪里等,您可能希望使用类似Google Analytics的内容。