我会尽量说清楚的,我在用“Users Following System“插件。我在author.php
profile,现在我尝试使用Ajax分页。
当然是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);
UPDATE 1 Ajax分页现在正在工作,但我仍然有同样的问题,当我单击“加载更多”时,我得到了以下管理员用户!!它应该为每个作者提供以下用户:(
这就是我在《作者》中所做的。php可以像@user141080那样获取以下用户列表。
<div id="following">
<div class="following-cont">
<?php
$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\' => \'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>\';
}
?>
</div><!-- #following -->
<div class="loadmore">More</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\', \'.loadmore\', 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>
功能。Ajax操作的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 \'Not followed anyone yet.\';
} 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();
}
}
那么现在我如何获得每个作者的以下用户,以及为什么我让管理员跟踪用户而不是他们的用户?
SO网友:user141080
问题是这一行:
$curauth = (isset($_GET[\'author_name\'])) ? get_user_by(\'slug\', $author_name) : get_userdata(intval($author));
首先,使用POST请求进行ajax调用,这就是GET数组为空的原因。其次,未定义变量$author。因此,每次调用脚本时,变量$curauth都是空的。
一种解决方案是,您可以通过ajax调用发送作者姓名,如页面或nonce。
编辑:在文件中author.php
您检测到作者数据($curauth
) 使用名称(get_user_by(\'slug\', $author_name)
) or 具有作者id(get_userdata(intval($author))
). 在服务器上执行相同操作(function.php
) 您必须通过ajax调用发送相同的数据。
<?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;
var canBeLoaded = true,
bottomOffset = 2000;
jQuery(function($)
{
$(window).scroll(function()
{
if( $(document).scrollTop() > ( $(document).height() - bottomOffset ) && canBeLoaded == true )
{
canBeLoaded = false;
/// add tmp_author_name and tmp_author to the ajax data array
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\').append(response);
canBeLoaded = true;
page++;
});
}
});
});
</script>
在服务器上读取post数据,然后检测作者。
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\'];
/// detect the author data with the name or with the author id
$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 \'Not followed anyone yet.\';
}
else
{
//// continue with your existing code. /////
}
}
else
{
echo \'Not followed anyone yet.\';
}
}