WordPress-如何仅显示10个用户?

时间:2018-12-13 16:55:39

标签: php wordpress author

对不起,我是Wordpress的新手,如何仅显示10个用户,而其他用户则“查看更多”。

在询问之前我搜索了更多次,但一无所获!

谢谢。

<?php
$post_id = get_the_ID();
$users_liked = get_post_meta( $post_id, '_user_liked', true );

if ( '' !== $users_liked && ! empty( $users_liked ) ) {
?>

<span>Liked:</span></br>
<?php foreach ( array_values( $users_liked ) as $user_id ) : 

$user = get_user_by( 'id', $user_id );

if ( false === $user ) {
  continue;
}
?>

<div class="sort-list-name">

    <a href="<?php echo get_author_posts_url($user->ID);?>">
        <?php echo get_avatar( $user->ID, 35 ); ?>  
    </a>

</div>

<?php

 endforeach; ?>

<?php } ?>

1 个答案:

答案 0 :(得分:0)

首先,所有尝试在循环中添加索引

<?php
$post_id = get_the_ID();
$i = 0;
$users_liked = get_post_meta( $post_id, '_user_liked', true );

if ( '' !== $users_liked && ! empty( $users_liked ) ) {
?>

<span>Liked:</span></br>
<?php foreach ( array_values( $users_liked ) as $user_id ) : 

$user = get_user_by( 'id', $user_id );

if ( false === $user ) {
  continue;
}
?>

<div class="sort-list-name">

    <a href="<?php echo get_author_posts_url($user->ID);?>">
        <?php echo get_avatar( $user->ID, 35 ); ?>  
    </a>

</div>

<?php

 if ($i++ == 9){
  echo "<a href='#'>see more</a>";
  break;
  } 

 endforeach; ?>

<?php } ?>

然后如果$ i得到9,则循环将中断