drbhjey445647 2017-03-29 09:32
浏览 29
已采纳

命令'rand'不能在Wordpress foreach循环上工作

I am trying to order some data by 'rand' on a foreach loop I have created which is outputting some users which I have created a custom role for. all the other bits are great apart from this.

This is the current state of the loop:

<?php

// Featured Consultants
$consultants = get_users( 'role=consultant&number=2' );
// Array of WP_User objects.
foreach ( $consultants as $consultant ) {

    // Check for 'featured' Consultants
    if($consultant->featured == 'Yes'){

        echo '<div class="columns six consultantCard">';

            echo '<div class="columns four">';
            echo get_avatar( $consultant->id, 150 );
            echo '</div>';

            echo '<div class="columns eight">';
                // Get users details
                echo '<span class="name">' . esc_html( $consultant->first_name ) . ' ' . esc_html( $consultant->last_name ) . '</span>';
                echo '<span class="jobTitle">' . esc_html( $consultant->job_title ) . '</span>';
                echo '<span class="location">Currently based in ' . esc_html( $consultant->current_location ) . '</span>';

                // Check if user is less than 30 days old
                if( strtotime($consultant->user_registered) < strtotime('30 days') ){   
                    echo '<span class="newTag">New</span>'; 
                }

                // Check if user is featured
                if($consultant->featured == 'Yes'){
                    echo '<span class="featuredTag">Featured Candiate</span>';
                }           

                // Check if user is accredited
                if($consultant->accredited == 'Yes'){ ?>
                    <span class="icon"><img src="<?php bloginfo('template_url');?>/assets/img/icons/accreditedIcon.png" /></span>
                    <?php echo '<span class="accreditedTag">Bench Accredited</span>';
                }
            echo '</div>';

        echo '</div>';  

    } else{
        echo 'No Featured Consultants';
    }
}
?>

It just appears to be showing the latest entries and nothing more. I tried to also create an array but still the same result like so:

$consultant = array(
'role' => 'consultant',
'order'     => 'rand'
); 
  • 写回答

1条回答 默认 最新

  • douyou1937 2017-03-29 09:45
    关注

    We can use the get_users() to get a list of authors, users with a specific role, a user with specific meta, etc. The function returns users which can be ordered by ID, login, nickname, email, URL, registered, display_name, post_count, or meta_value. But there is no random option such as what get_posts() function provides to shows posts randomly.

    Since the get_users() function uses WP_User_Query class, there is an action hook pre_user_query we can use to modify the class variable. The idea is using our own 'rand' order by the parameter. If we put 'rand' to the orderby parameter, user_login will be used instead. In this case, we need to replace it with RAND() to result in users randomly. In this example below, we I ‘rand’ and you can use your own order by name.

    // Add this code in your function.php
    add_action( 'pre_user_query', 'my_random_user_query' );
    
    function my_random_user_query( $class ) {
        if( 'rand' == $class->query_vars['orderby'] )
            $class->query_orderby = str_replace( 'user_login', 'RAND()', $class->query_orderby );
    
        return $class;
    }
    

    The WP_User_Query contains order by query and the our arguments. Now, we have a new order by parameter to WordPress.

    $users = get_users( array(
        'orderby' => 'rand',
        'number'  => 5
    ));
    print_r( $users );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制