dongshi9407 2019-06-18 09:38
浏览 37

wordpress如何从user_meta数组值中删除post_ID

I have created a "likes Posts" button that users can click to save a post to their list of "liked." The button appends the current post ID to a user_meta field. But when the user clicks the button again, how does post ID get deleted from user_meta array and the chris_likes value -1

functions.php

<?php 
add_action('wp_ajax_nopriv_chris_like_post', 'chris_like_post');
add_action('wp_ajax_chris_like_post', 'chris_like_post');
function chris_like_post(){
    global $user_ID;

    $postID = $_REQUEST['post'];

    if( is_numeric( $postID ) ){
        $count = get_post_meta($postID, 'chris_likes' , true);
        if( empty($count) || $count == '' ) $count = 0;

        $count++;
        if ( $user_ID ) {
            $user_liked = get_the_author_meta( 'liked', $user_ID  );
            $user_liked_posts = explode( ',' , $user_liked);

            if( empty($user_liked) ){
                update_user_meta( $user_ID, 'liked', $postID );
                update_post_meta( $postID, 'chris_likes', $count );
            }
            else{
                if( !in_array($postID , $user_liked_posts) ){
                    update_post_meta( $postID, 'chris_likes', $count );
                    $postID = $user_liked.','.$postID;
                    update_user_meta( $user_ID , 'liked' , $postID );
                }
            }
        }else{
            $user_liked = $_COOKIE["chris_likes_".$postID];
            if( empty($user_liked) ){
                setcookie( 'chris_likes_'.$postID , $postID , time()+999999 , '/');
                update_post_meta( $postID, 'chris_likes', $count );
            }
        }

        echo $count ;
    }
    die; 

}
?>

the page template

<?php
global $post ,$postID; $postID = $_REQUEST['post']; 
    $count = get_post_meta($post->ID, 'chris_likes', true);
    $active =  $liked = false ;
    if ( $user_ID ) {
        $user_liked = get_the_author_meta( 'liked' , $user_ID ) ;
        $user_liked_posts = explode( ',' , $user_liked);
        if( in_array( $post->ID , $user_liked_posts) ){ 
        $active = ' active';
        $liked = '';
        }
        else{
        $active = '';
        $liked = '';
        }
    }
    else{
        $user_liked = $_COOKIE["chris_likes_".$post->ID] ;

        if( !empty($user_liked) ){
            $active = ' active';
            $liked = __( '' , 'chris');
        }

    }
?>

<div class="card-count">
    <div class="like">
        <a href="javascript:;" class="chris-likes<?php echo $active; ?> iconfont icon-seed-filled" rel="<?php echo $post->ID ?>" title="<?php echo $liked; ?>">
            <span class="count-num"><?php if( empty($count) ) echo "0"; else  echo $count; ?></span>
        </a>
    </div>
</div>

the js

jQuery(document).ready(function() {
        jQuery(document).on("click", "a.chris-likes",
        function() {
                var post_id = jQuery(this).attr('rel');
                var like = jQuery(this);
                var rateHolder = $(this).children('.count-num');

                if (like.hasClass('active')) {
                        $.post("/wp-admin/admin-ajax.php", {
                                action: 'chris_like_post',
                                post: post_id
                        },
                        function(data) {
                                like.removeClass('active').attr('title', '');
                                $(rateHolder).html(data);
                        },
                        'html');
                } else {
                        $.post("/wp-admin/admin-ajax.php", {
                                action: 'chris_like_post',
                                post: post_id
                        },
                        function(data) {
                                like.addClass('active').attr('title', '');
                                $(rateHolder).html(data);
                        },
                        'html');
                }
        });
});

</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
    • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
    • ¥15 Arcgis相交分析无法绘制一个或多个图形
    • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
    • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
    • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
    • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
    • ¥30 3天&7天&&15天&销量如何统计同一行
    • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
    • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型