Wordpress - Users list with Ajax Pagination within Author Profile

萝らか妹 提交于 2019-12-11 07:58:21

问题


I will try to be clear this time, I'm using "Users Following System" plugin. And I added a list of the following/followers users into the author.php profile, now I'm trying to use Ajax pagination.

For sure into author.php I used this line to get the users information.

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

And this line to get the user meta of _pwuf_following

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

This is what i did in author.php to get the list of following users.

<?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>';
}
?>

And this is the JS to get Ajax action also in author.php

<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>

And this is from function.php of the Ajax handler.

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();
}

No errors but not working, I get "Not followed anyone yet." instead of the following users, what i did wrong, Thanks.

来源:https://stackoverflow.com/questions/52606104/wordpress-users-list-with-ajax-pagination-within-author-profile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!