Wordpress 为大多数查看的帖子创建插件问题? [英] Wordpress creating plugin for most viewed posts problem?

查看:30
本文介绍了Wordpress 为大多数查看的帖子创建插件问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想创建一个插件,当访问者(用户,访问者,...)访问某个帖子时,记住什么帖子,并增加该帖子的计数器,我写了这段代码,但有时,计数器会增加,甚至没有查看帖子,或者将带有其他 ID 的帖子添加到表格中.有人可以帮我解决这个问题吗?我知道我正在尝试使用一些插件,但仍然想编写这个插件.

I just want to create plugin that will when visitor(user,visitor,...) visit some post,remember what post,and to increment counter of that post,I wrote this code,but sometimes,counter is incremented,even post isn't viewed,or post with other Id is added to a table.Can someone help me with this,please.I know that there are plugins for this that I'm trying to do,but still want to write this plugin.

function IncrementPostCount($the_content) {
    global $post;
    global $wpdb;

    if(($post->post_status == 'publish') && (int)$post->ID) {

        if(is_single()) { // just for single post - not for page
            $postID = (int)$post->ID;

            $postTitle = urlencode($post->post_title);
            $postLink = urlencode(get_permalink($post->ID));

            $oneRow = $wpdb->get_row("SELECT * FROM wp_postovi WHERE postAjDi='$postID'");

            if(empty ($oneRow)) {
                $postCounter = 1;
                $data_array = array(
                   'readnTimes' => $postCounter, 
                   'linkPost'=>$postLink, 
                   'TitlePost'=>$postTitle,
                   'postAjDi'=>$postID);
                $wpdb->insert('wp_najcitaniji_postovi', $data_array);                
            }
            else {
                $postCounter = intval($oneRow->readnTimes) + 1;
                $data_array = array('readnTimes' => $postCounter);
                $where_array = array('postAjDi'=>intval($oneRow->postAjDi));
                $wpdb->update('wp_postovi',$data_array,$where_array);
            }

            return $the_content;
        }
        return $the_content;
    }
}
add_filter('the_content','IncrementPostCount');

抱歉我的英语不好,提前tnx.

Sorry on my bad english,tnx in advance.

推荐答案

以下是如何使用 postmeta 表来做到这一点.

Here's how to do that with the postmeta table.

function IncrementPostCount(){
  if(is_single()){
    global $wp_query;
    $count = get_post_meta( $wp_query->post->ID, 'readnTimes', true );
    $count = empty($count) ? 1 : $count + 1;
    add_post_meta($wp_query->post->ID, 'readnTimes', $count, true) or update_post_meta($wp_query->post->ID, 'readnTimes', $count);
  }
}
add_action( 'template_redirect', 'IncrementPostCount' );

另外,最好早点把它挂起来.这样,每次页面加载时计数只会增加一次(the_content 可以在每个页面上多次触发,即使在单个页面上也是如此.template_redirect 每个请求只触发一次).此外,如果您将数据存储在 template_redirect 中,您可以在模板中使用更新后的观看次数,为访问者提供更准确的观看次数.

Also, it's better to hook it in earlier. That way, the count is only incremented once per page load (the_content can be fired multiple times per page, even on a single page. template_redirect only fires once per request). Also, if you store the data at template_redirect, you can use the updated view count in the template, giving your visitors an even more accurate view count.

而且您无需担心数据库表、自定义 SQL 或任何这些.

And you don't need to worry about database tables, custom SQL, or any of that.

这篇关于Wordpress 为大多数查看的帖子创建插件问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆