首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >当回波跟随Ajax按钮在Ajax分页模板不工作?

当回波跟随Ajax按钮在Ajax分页模板不工作?
EN

WordPress Development用户
提问于 2019-02-22 19:30:56
回答 1查看 155关注 0票数 0

好吧,这次我会尽量说清楚的,我真的需要你们的帮助。

我使用一个与Ajax一起工作的插件来创建一个“跟随”按钮,让我们的用户互相跟踪。

我把下面的按钮放在我需要一个用户跟踪其他用户的任何地方,就像那个echo pwuf_get_follow_unfollow_links( //user id here );

我面临的问题是,如果我在任何Ajax分页模板中添加了这个回显函数--无论是在Ajax分页后还是用户列出Ajax分页,下面的按钮都不能工作,并且在单击时找不到用户id!

但是可以肯定的是,在我将更多信息加载到第二个页面之前,他可以在第一个页面上找到用户id!

例句:如果我有一个类似于这个Ajax的分页,并且试图在这个ajax分页中回显下面的按钮函数,那么这个页面就不能工作了!?

代码语言:javascript
代码运行次数:0
运行
复制
add_action('wp_ajax_loadmore_by_ajax', 'loadmore_by_ajax_callback');
add_action('wp_ajax_nopriv_loadmore_by_ajax', 'loadmore_by_ajax_callback');

function loadmore_by_ajax_callback()  {
   //do something

   //echoing follow button
    echo pwuf_get_follow_unfollow_links( //user id here );

}

我将在下面添加我的所有插件代码,我希望得到一些帮助来解决这个问题,谢谢您的时间。

follow.js

代码语言:javascript
代码运行次数:0
运行
复制
jQuery(document).ready(function($) {
    /*******************************
    follow / unfollow a user
    *******************************/
    $( '.follow-links a' ).on('click', function(e) {
        e.preventDefault();

        var $this = $(this);

        if( pwuf_vars.logged_in != 'undefined' && pwuf_vars.logged_in != 'true' ) {
            alert( pwuf_vars.login_required );
            return;
        }

        var data      = {
            action:    $this.hasClass('follow') ? 'follow' : 'unfollow',
            user_id:   $this.data('user-id'),
            follow_id: $this.data('follow-id'),
            nonce:     pwuf_vars.nonce
        };

        $this.closest('.follow-links').find('img.pwuf-ajax').show();

        $.post( pwuf_vars.ajaxurl, data, function ( response ) {
            if ( response == 'success' ) {
                if ( $this.hasClass( 'follow' ) ) {;
                    $this.removeClass( 'follow' ).addClass( 'unfollow' );
                    $this.find( 'span' ).text( 'Unfollow' );
                } else {;
                    $this.removeClass( 'unfollow' ).addClass( 'follow' );
                    $this.find( 'span' ).text( 'Follow' );
                }
            } else {
                alert( pwuf_vars.processing_error );
            }
            $this.closest('.follow-links').find('img.pwuf-ajax').hide();
        });
    });
});

actions.php

代码语言:javascript
代码运行次数:0
运行
复制

display-functions.php

代码语言:javascript
代码运行次数:0
运行
复制

follow-functions.php

代码语言:javascript
代码运行次数:0
运行
复制
 $follow ) {
            if ( $follow == $unfollow_user ) {
                unset( $following[$key] );
                $modified = true;
            }
        }

        if ( $modified ) {
            if ( update_user_meta( $user_id, '_pwuf_following', $following ) ) {
                pwuf_decrease_followed_by_count( $unfollow_user );
            }
        }

    }

    // get all IDs that follow the user we have just unfollowed so that we can remove $user_id
    $followers = pwuf_get_followers( $unfollow_user );

    if ( is_array( $followers ) && in_array( $user_id, $followers ) ) {

        $modified = false;

        foreach ( $followers as $key => $follower ) {
            if ( $follower == $user_id ) {
                unset( $followers[$key] );
                $modified = true;
            }
        }

        if ( $modified ) {
            update_user_meta( $unfollow_user, '_pwuf_followers', $followers );
        }

    }

    if ( $modified ) {
        do_action( 'pwuf_post_unfollow_user', $user_id, $unfollow_user );
        return true;
    }

    return false;
}



/**
 * Retrieve following count
 *
 * Gets the total number of users that the specified user is following
 *
 * @access      private
 * @since       1.0
 * @param   int $user_id - the ID of the user to retrieve a count for
 * @return      int
 * @param  int|string|WP_User $user_id User ID or object.
 * @return bool                        Whether the user exists
 */

function pwuf_get_following_count( $user_id = 0 ) {

    if ( empty( $user_id ) ) {
        $user_id = get_current_user_id();
    }

    $following = pwuf_get_following( $user_id );

    $count = 0;

    if ( $following ) {
        $count = count( $following );
    }

    return (int) apply_filters( 'pwuf_get_following_count', $count, $user_id );
}


/**
 * Retrieve follower count
 *
 * Gets the total number of users that are following the specified user
 *
 * @access      private
 * @since       1.0
 * @param   int $user_id - the ID of the user to retrieve a count for
 * @return      int
 */

function pwuf_get_follower_count( $user_id = 0 ) {

    if ( empty( $user_id ) ) {
        $user_id = get_current_user_id();
    }

    $followed_count = get_user_meta( $user_id, '_pwuf_followed_by_count', true );

    $count = 0;

    if ( $followed_count ) {
        $count = $followed_count;
    }

    return (int) apply_filters( 'pwuf_get_follower_count', $count, $user_id );
}



/**
 * Increase follower count
 *
 * Increments the total count for how many users a specified user is followed by
 *
 * @access      private
 * @since       1.0
 * @param   int $user_id - the ID of the user to increease the count for
 * @return      int
 */

function pwuf_increase_followed_by_count( $user_id = 0 ) {

    do_action( 'pwuf_pre_increase_followed_count', $user_id );

    $followed_count = pwuf_get_follower_count( $user_id );

    if ( $followed_count !== false ) {

        $new_followed_count = update_user_meta( $user_id, '_pwuf_followed_by_count', $followed_count + 1 );

    } else {

        $new_followed_count = update_user_meta( $user_id, '_pwuf_followed_by_count', 1 );

    }

    do_action( 'pwuf_post_increase_followed_count', $user_id );

    return $new_followed_count;
}


/**
 * Decrease follower count
 *
 * Decrements the total count for how many users a specified user is followed by
 *
 * @access      private
 * @since       1.0
 * @param   int $user_id - the ID of the user to decrease the count for
 * @return      int
 */

function pwuf_decrease_followed_by_count( $user_id ) {

    do_action( 'pwuf_pre_decrease_followed_count', $user_id );

    $followed_count = pwuf_get_follower_count( $user_id );

    if ( $followed_count ) {

        $count = update_user_meta( $user_id, '_pwuf_followed_by_count', ( $followed_count - 1 ) );

        do_action( 'pwuf_post_increase_followed_count', $user_id );

    }
    return $count;
}


/**
 * Check if a user is following another
 *
 * Increments the total count for how many users a specified user is followed by
 *
 * @access      private
 * @since       1.0
 * @param   int $user_id       - the ID of the user doing the following
 * @param   int $followed_user - the ID of the user to check if being followed by $user_id
 * @return      int
 */

function pwuf_is_following( $user_id, $followed_user ) {

    $following = pwuf_get_following( $user_id );

    $ret = false; // is not following by default

    if ( is_array( $following ) && in_array( $followed_user, $following ) ) {
        $ret = true; // is following
    }

    return $ret;

}

scripts.php

代码语言:javascript
代码运行次数:0
运行
复制
 __( 'There was a problem processing your request.', 'pwuf' ),
        'login_required'   => __( 'Oops, you must be logged-in to follow users.', 'pwuf' ),
        'logged_in'        => is_user_logged_in() ? 'true' : 'false',
        'ajaxurl'          => admin_url( 'admin-ajax.php' ),
        'nonce'            => wp_create_nonce( 'follow_nonce' )
    ) );
}
add_action( 'wp_enqueue_scripts', 'pwuf_load_scripts' );
EN

回答 1

WordPress Development用户

回答已采纳

发布于 2019-02-22 20:04:10

我认为,如果您更改您的jQuery单击操作选择器来处理事件委托,那么下面的按钮在ajax添加之后应该可以工作。

代码语言:javascript
代码运行次数:0
运行
复制
// Change this
$( '.follow-links a' ).on('click', function(e) {
// to this
$( '.some-parent-not-added-by-ajax' ).on('click', '.follow-links a', function(e) {
// or as last resort
$( document ).on('click', '.follow-links a', function(e) {
票数 1
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/329632

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档