我有这个短代码来显示按注册日期排序的用户头像循环。
function Profiles() {
$args = array(
\'orderby\' => \'registered\',
\'order\' => \'DESC\',
\'fields\' => \'all_with_meta\',
);
$user_query = new WP_User_Query( $args );
if ( ! empty( $user_query->results ) ) {
foreach ( $user_query->results as $user ) { ?>
<div class="user-item user-img-<?php echo $user->ID; ?>" rel="<?php echo $user->ID; ?>">
<a href="<?php echo $website; ?>" rel="<?php echo $user->ID; ?>" target="_blank"><?php echo get_avatar( $user->ID, 100 ); ?></a>
</div>
<?php }
} else {
echo \'No users found.\';
}
}
add_shortcode(\'profiles\', \'Profiles\');
如果任何用户角色更改为
sticky
(自定义角色),然后用户移动到列表的顶部。对于循环中的所有其他用户,默认orderby应保持为已注册状态。一旦
sticky
用户角色被删除,它将返回到正常排序。
我仔细考虑了一下,也许我可以把两个论点结合在一起Wp_User_Query
那可能有用吗?
例如$sticky_args
有role__in => \'sticky\'
和$simple_args
拥有所有其他roles => \'subscriber\', \'customer\', \'author\'
或role__not_in => \'sticky\'
论点然后结合这个Wp_User_Query
在某种程度上$user_query->results
显示$sticky_args
首先列出用户,然后$simple_args
然后在循环中列出用户列表。我尝试了几个代码,并试图思考if语句,但到目前为止什么都不起作用。谷歌也没有帮助。非常感谢您的帮助。
最合适的回答,由SO网友:Ali 整理而成
我通过在每个循环之后添加两个循环来解决这个问题。第一个循环显示用户\'role__in\' => \'sticky\'
而第二个循环显示用户\'role__not_in\' => \'sticky\'
它工作得很好。这是代码,如果它对任何人都有帮助的话。
function Profiles() {
echo "<div class=\'users-list\'>"
// First loop with sticky users
$args = array(
\'role__in\' => \'sticky\',
\'orderby\' => \'registered\',
\'order\' => \'DESC\',
\'fields\' => \'all_with_meta\',
);
$user_query = new WP_User_Query( $args );
if ( ! empty( $user_query->results ) ) {
foreach ( $user_query->results as $user ) { ?>
<div class="user sticky-user user-img-<?php echo $user->ID; ?>" rel="<?php echo $user->ID; ?>">
<a href="<?php echo $website; ?>" rel="<?php echo $user->ID; ?>" target="_blank"><?php echo get_avatar( $user->ID, 100 ); ?></a>
</div>
<?php }
}
// Second loop without sticky users
$args = array(
\'role__not_in\' => \'sticky\',
\'orderby\' => \'registered\',
\'order\' => \'DESC\',
\'fields\' => \'all_with_meta\',
);
$user_query = new WP_User_Query( $args );
if ( ! empty( $user_query->results ) ) {
foreach ( $user_query->results as $user ) { ?>
<div class="user user-img-<?php echo $user->ID; ?>" rel="<?php echo $user->ID; ?>">
<a href="<?php echo $website; ?>" rel="<?php echo $user->ID; ?>" target="_blank"><?php echo get_avatar( $user->ID, 100 ); ?></a>
</div>
<?php }
} else {
echo \'No users found.\';
}
echo "</div>"
}
add_shortcode(\'profiles\', \'Profiles\');