WordPress-用户列表,其中每个作者个人资料中都有其关注者和关注用户的Ajax分页

时间:2019-02-03 17:40:14

标签: ajax wordpress pagination author

我正在尝试使用Ajax分页在其个人资料页面中为每个用户创建关注者/关注用户列表。

这里有人帮助我使Ajax分页正常工作,但是我遇到了一个大问题,我无法解决!

我面临的问题是我们所有的用户个人资料页面中的内容,当他们单击“加载更多”按钮时,他们正在吸引遵循管理员ID的用户!

在author.php内部,我使用此行获取作者信息。

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

那么如何使他们吸引他们的关注用户?

这是我完成的所有代码。

author.php

<div id="following">

    <div class="following-cont">
<?php

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

if ( empty( $include ) ) {

     echo '<div class="no-follower">Not found followers yet <i class="fa fa-slack fa-1x" aria-hidden="true"></i></div>';

    } else {

$args = array (
    'order' => 'DESC',
    'include'  => $include,
    'number'  => 6,
    '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>';
    }

?>

<?php
// The variables tmp_author_name and tmp_author serve only as temporary storage.
// They controls how the data should be determined on the server.
$tmp_author_name = '';
$tmp_author = 0;
if( isset($_GET['author_name']) )
{
    $tmp_author_name = $author_name;
}
else
{
    $tmp_author = intval($author);  
}
?>

<script type="text/javascript">
var ajaxurl = "<?php echo admin_url( 'admin-ajax.php' ); ?>";
var page = 2;
jQuery(function($) {
    $('body').on('click', '.loadmoreartists', function() {
            var data = 
            {
                'action': 'user_following_by_ajax',
                'page': page,
                'security': '<?php echo wp_create_nonce("user_more_following"); ?>',
                'author_name': '<?php echo esc_html($tmp_author_name); ?>',
                'author': '<?php echo esc_html($tmp_author); ?>'

            };

        $.post(ajaxurl, data, function(response) {

        $('.following-cont').append(response);
        page++;

        });
    });
});
</script>

</div>

<div class="loadmoreartists">More</div>

</div><!-- #following -->

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 = NULL;

    if( isset($_POST['author_name']) AND trim($_POST['author_name']) != '' )
    {
        $curauth = get_user_by('slug', trim($_POST['author_name']) );
    }
    elseif( isset($_POST['author']) AND intval($_POST['author']) > 0 )
    {
        $curauth = get_userdata(intval($_POST['author']));   
    }

    if( !is_null($curauth) ) {

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

if ( empty( $include ) ) {

     echo '<div class="no-follower">Not found followers yet <i class="fa fa-slack fa-1x" aria-hidden="true"></i></div>';

    } else {

$args = array (
    'order' => 'DESC',
    'include'  => $include,
    'number'  => 6,
    '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();
    }
}

0 个答案:

没有答案