Wordpress - 关注用户列表 Ajax

Wordpress - Following users list with Ajax

我正在使用“Users Following System”插件。到目前为止,我将 following/followers 的列表添加到 author.php 配置文件中,现在我正在尝试对此使用 Ajax 但我面临的问题是我需要得到相同的user meta 为每个用户按ID,让每个人都可以看到彼此的以下用户。

author.php中,我使用这一行来获取用户信息。

<?php
    $curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
?>

这行获取 _pwuf_following

的用户元数据
$include = get_user_meta($curauth->ID, '_pwuf_following', true);

但是当我在 Ajax 函数处理程序中添加相同的行时,它不起作用。

我试过了get_queried_object();wp_get_current_user();get_userdata();
但我总是失败。

这是来自 author.php 的代码片段,用于获取关注用户的列表。

<?php

$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));

$include = get_user_meta($curauth->ID, '_pwuf_following', true);

if ( empty( $include ) ) {

     echo 'Not followed anyone yet.';

    } else {

$args = array (
    'order' => 'DESC',
    'include'  => $include,
    'number'  => 52,
    'paged' => 1
);

$wp_user_query = new WP_User_Query( $args );

$users = $wp_user_query->get_results();

    echo '<div id="top-artists-contributors-3">';
    echo '<ul id="grid-contributors-4">';
    echo '<li class="scroll-artists">';
    foreach ( $users as $user ) {
        $avatar_size = 90;
        $avatar = get_avatar($user->user_email, 200);
        $author_profile_url = get_author_posts_url($user->ID);
        $profile = get_userdata($user->ID);

    echo '<div class="single-item-3">';
    echo '<div class="author-gravatar-3"><a href="', $author_profile_url, '">', $avatar , '</a></div>';
    echo '<div class="members-name"><a href="', $author_profile_url, '">' . $profile->first_name .'</a></div>';
    echo '</div>';           
    }
    echo '</li>';
    echo '</ul>';
    echo '</div>';
}
?>

这是获取 Ajax url 和 action

的 js
<script type="text/javascript">
var ajaxurl = "<?php echo admin_url( 'admin-ajax.php' ); ?>";
var page = 2;
var canBeLoaded = true,
bottomOffset = 2000;

jQuery(function($) {
$(window).scroll(function() {
    if( $(document).scrollTop() > ( $(document).height() - bottomOffset ) && canBeLoaded == true ) {
    canBeLoaded = false;
        var data = {
            'action': 'user_following_by_ajax',
            'page': page,
            'security': '<?php echo wp_create_nonce("user_more_following"); ?>'
        };
        $.post(ajaxurl, data, function(response) {
            $('#following').append(response);
            canBeLoaded = true;
            page++;
        });
    }
});
});
</script>

这是来自 Ajax 处理程序的 function.php。

add_action('wp_ajax_user_following_by_ajax', 'user_following_by_ajax_callback');
add_action('wp_ajax_nopriv_user_following_by_ajax', 'user_following_by_ajax_callback');

function user_following_by_ajax_callback() {
    check_ajax_referer('user_more_following', 'security');
    $paged = $_POST['page'];

$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));

$include = get_user_meta($curauth->ID, '_pwuf_following', true);


if ( empty( $include ) ) {

     echo 'Not followed anyone yet.';

    } else {

$args = array (
    'order' => 'DESC',
    'include'  => $include,
    'number'  => 52,
    'paged' => $paged
);

$wp_user_query = new WP_User_Query( $args );

$users = $wp_user_query->get_results();

    echo '<div id="top-artists-contributors-3">';
    echo '<ul id="grid-contributors-4">';
    echo '<li class="scroll-artists">';
    foreach ( $users as $user ) {
        $avatar_size = 90;
        $avatar = get_avatar($user->user_email, 200);
        $author_profile_url = get_author_posts_url($user->ID);
        $profile = get_userdata($user->ID);

    echo '<div class="single-item-3">';
    echo '<div class="author-gravatar-3"><a href="', $author_profile_url, '">', $avatar , '</a></div>';
    echo '<div class="members-name"><a href="', $author_profile_url, '">' . $profile->first_name .'</a></div>';
    echo '</div>';           
    }
    echo '</li>';
    echo '</ul>';
    echo '</div>';
    }
    wp_die();
}

我看到您没有传递代码中使用的 author_name 字段。请检查下面的代码,我在其中添加了缺失的字段。

           <script type="text/javascript">
            var ajaxurl = "<?php echo admin_url( 'admin-ajax.php' ); ?>";
            var page = 2;
            var canBeLoaded = true,
            bottomOffset = 2000;


            jQuery(function($) {
            $(window).scroll(function() {
                if( $(document).scrollTop() > ( $(document).height() - bottomOffset ) && canBeLoaded == true ) {
                canBeLoaded = false;
                    var data = {
                        'action': 'user_following_by_ajax',
                        'data': { page : 'page', author_name: '<?php echo get_the_author(); ?>' }, // Here set author name which you are getting.
                        'security': '<?php echo wp_create_nonce("user_more_following"); ?>'
                    };
                    $.post(ajaxurl, data, function(response) {
                        $('#following').append(response);
                        canBeLoaded = true;
                        page++;
                    });
                }
            });
            });
            </script>