需要随机显示6位作者的名单

时间:2019-01-21 19:45:37

标签: php wordpress function

有-我已经很了解我为https://annexteenclinic.org改编的这个功能;但是,与其在网站上随机抽取20位左右作者中的6位作者,不如按字母顺序显示前6位。

很明显,我缺少了一些东西!我很好奇我如何调整下面的代码以随机抽取6位作者,然后显示它们。预先感谢您的想法。欢迎提出有关使此代码更整洁的想法。它正在蔓延。

function stahom() {
    $display_admins = false;
    //$order_by = 'display_name';
    // 'nicename', 'email', 'url', 'registered', 'display_name', or 'post_count'
    $order_by = 'rand';
    $order = 'ASC';
    $role = 'staff-directory'; // 'subscriber', 'contributor', 'editor', 'author' - leave blank for 'all'
    $hide_empty = false; // hides authors with zero posts
    $number = '6';

    if (!empty($display_admins)) {
        $blogusers = get_users('orderby='.$order_by.'&role='.$role);
    } else {
        $admins = get_users('role=administrator');
        $exclude = array();

        foreach ($admins as $ad) {
            $exclude[] = $ad->ID;
        }

        $exclude = implode(',', $exclude);
        $blogusers = get_users('exclude='.$exclude.'&orderby='.$order_by.'&number='.$number.'&order='.$order.'&role='.$role);
    }

    $authors = array();

    foreach ($blogusers as $bloguser) {
        $user = get_userdata($bloguser->ID);

        if (!empty($hide_empty)) {
            $numposts = count_user_posts($user->ID);
            if ($numposts < 1) {
                continue;
            }
        }

        $authors[] = (array) $user;
    }

    shuffle($authors);
    array_rand($authors);
    foreach ($authors as $author) {
        $author_title = get_field('title', 'user_'. $author['ID']);
        $display_name = $author['data']->display_name;
        $author_profile_url = get_author_posts_url($author['ID']);

        $author_regpho = get_field('regpho', 'user_'. $author['ID']);
        $regurl = $author_regpho['url'];

        $author_hovpho = get_field('hovpho', 'user_'. $author['ID']);
        $hovurl = $author_hovpho['url'];

        $size = 'thumbnail';
        $regthumb = $author_regpho['sizes'][$size];
        $hovthumb = $author_hovpho['sizes'][$size];

        echo '<div class="third"><div class="container"><a href="', $author_profile_url, '" class="contributor-link"><img src="', $regthumb,'" alt="', $display_name,'" class="image">';
        echo '<div class="overlay">';
        echo '<img src="', $hovthumb,'" alt="', $display_name,'" class="image"></div>';
        echo '</a></div>';
        echo '<div class="stalin"><a href="', $author_profile_url, '" class="contributor-link">', $display_name, '<br/><span>', $author_title, '</a></span></div></div>';
    }
}
add_action('hook_bottom_stahom', 'stahom');

1 个答案:

答案 0 :(得分:0)

非常感谢您的建议。我接受了您的建议,并在下面发布了代码。我所做的是摆脱了数量限制,然后按照建议将切片添加到随机抽取的前六个切片中。结果就是这了!感谢帮助!

function stahom() {
?>
<?php
 $display_admins = false;
 //$order_by = 'display_name';
 // 'nicename', 'email', 'url', 'registered', 'display_name', or 'post_count'
 $order_by = 'rand';
 $order = 'ASC';
 $role = 'staff-directory'; // 'subscriber', 'contributor', 'editor', 'author' - leave blank for 'all'
 $hide_empty = false; // hides authors with zero posts

 if(!empty($display_admins)) {
      $blogusers = get_users('orderby='.$order_by.'&role='.$role);
 } else {
    $admins = get_users('role=administrator');
    $exclude = array();

    foreach($admins as $ad) {
      $exclude[] = $ad->ID;
    }

    $exclude = implode(',', $exclude);
    $blogusers = get_users('exclude='.$exclude.'&orderby='.$order_by.'&number='.$number.'&order='.$order.'&role='.$role);
 }

 $authors = array();

 foreach ($blogusers as $bloguser) {
    $user = get_userdata($bloguser->ID);

    if(!empty($hide_empty)) {
        $numposts = count_user_posts($user->ID);
        if($numposts < 1) continue;
    }

    $authors[] = (array) $user;
 }

 shuffle($authors);$authors = array_slice( $authors, 0, 6 );array_rand($authors);foreach($authors as $author) {
    $author_title = get_field('title', 'user_'. $author['ID']);
    $display_name = $author['data']->display_name;
    $author_profile_url = get_author_posts_url($author['ID']);

    $author_regpho = get_field('regpho', 'user_'. $author['ID']);
    $regurl = $author_regpho['url'];

    $author_hovpho = get_field('hovpho', 'user_'. $author['ID']);
    $hovurl = $author_hovpho['url'];

    $size = 'thumbnail';
    $regthumb = $author_regpho['sizes'][ $size ];
    $hovthumb = $author_hovpho['sizes'][ $size ];

    echo '<div class="third"><div class="container"><a href="', $author_profile_url, '" class="contributor-link"><img src="', $regthumb,'" alt="', $display_name,'" class="image">';
    echo '<div class="overlay">';
    echo '<img src="', $hovthumb,'" alt="', $display_name,'" class="image"></div>';
    echo '</a></div>';
    echo '<div class="stalin"><a href="', $author_profile_url, '" class="contributor-link">', $display_name, '<br/><span>', $author_title, '</a></span></div></div>';
}

?>
<?php
}
add_action('hook_bottom_stahom', 'stahom');