0

我正在修改一些代码,以便让它注册一个对不可见 div 元素的点击,它扩展了一篇文章以显示它的摘录,同时为它被任何人点击的次数添加 +1。之后,它使用 ajax 更新一个元素,并使用它收到的点击次数。

至少,这是目标。

以下代码最终破坏了 Wordpress,并给了我厄运的白屏。这取自一个简单的点击计数器,带有一个 Ajax 回调来更新数字。

我的问题在于破解它以注册对不同元素的点击。

在不浪费任何人太多时间的情况下,这是我的问题:

我不需要将所有重命名post_likepost_reader吗?有人亲自告诉我,should work so check your server但这很荒谬……似乎。

请注意,在您看到的下方post_reader,它之前已经说过post_like

// post click to expand button

$timebeforerevote = 1;

add_action('wp_ajax_nopriv_post-like', 'post_reader');
add_action('wp_ajax_post-like', 'post_reader');

wp_localize_script('like_post', 'ajax_var', array(
    'url' => admin_url('admin-ajax.php'),
    'nonce' => wp_create_nonce('ajax-nonce')
));

function post_like()
{
    $nonce = $_POST['nonce'];

    if ( ! wp_verify_nonce( $nonce, 'ajax-nonce' ) )
        die ( 'Busted!');

    if(isset($_POST['post_reader']))
    {
        $ip = $_SERVER['REMOTE_ADDR'];
        $post_id = $_POST['post_id'];

        $meta_IP = get_post_meta($post_id, "voted_IP");

        $voted_IP = $meta_IP[0];
        if(!is_array($voted_IP))
            $voted_IP = array();

        $meta_count = get_post_meta($post_id, "votes_count", true);

        if(!hasAlreadyVoted($post_id))
        {
            $voted_IP[$ip] = time();

            update_post_meta($post_id, "voted_IP", $voted_IP);
            update_post_meta($post_id, "votes_count", ++$meta_count);

            echo $meta_count;
        }
        else
            echo "already";
    }
    exit;
}

function hasAlreadyVoted($post_id)
{
    global $timebeforerevote;

    $meta_IP = get_post_meta($post_id, "voted_IP");
    $voted_IP = $meta_IP[0];
    if(!is_array($voted_IP))
        $voted_IP = array();
    $ip = $_SERVER['REMOTE_ADDR'];

    if(in_array($ip, array_keys($voted_IP)))
    {
        $time = $voted_IP[$ip];
        $now = time();

        if(round(($now - $time) / 60) > $timebeforerevote)
            return false;

        return true;
    }

    return false;
}

function getPostReadLink($post_id)
{
    $themename = "toolbox";

    $vote_count = get_post_meta($post_id, "votes_count", true);

    $output = '<div class="post-read">';
    if(hasAlreadyVoted($post_id))
        $output .= ' <span title="'.__('I like this article', $themename).'" class="qtip like alreadyvoted"></span>';
    else
        $output .= '<a href="#" data-post_id="'.$post_id.'">
                    <span  title="'.__('I like this article', $themename).'"class="qtip like"></span>
                </a>';
    $output .= '<span class="count">'.$vote_count.'</span></div>';

    return $output;
}

点击时调用的函数:

jQuery(".expand").click(function(e){

    e.preventDefault();

    readers = jQuery(this);

    // Retrieve post ID from data attribute
    post_id = readers.data("post_id");

    // Ajax call
    jQuery.ajax({
        type: "post",
        url: ajax_var.url,
        data: "action=post-reader&nonce="+ajax_var.nonce+"&post_reader=&post_id="+post_id,
        success: function(count){
            // If vote successful
            if(count != "already")
            {
                heart.addClass("readered");
                heart.siblings(".count").text(count);
            }
        }
    });

    return false;
})

在适当的 div 中调用它。

<?php echo getPostReadLink(get_the_ID());?>
4

0 回答 0