Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPBカップ
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

WordPressで登録ユーザーをランダムに表示する方法

WordPressサイトで登録ユーザーを紹介したいですか?

ユーザーを強調することで、コミュニティ意識を醸成し、会員数を増やすことができます。しかし、多くのメンバーがいるサイトでは、数人のユーザーをランダムに表示するだけでも意味があります。

この投稿では、WordPressサイトにランダムな登録ユーザーのリストを表示する方法を紹介します。

How to Randomly Display Registered Users in WordPress

WordPressで登録ユーザーをランダムに表示する理由とは?

ユーザー登録が可能なWordPressサイトを運営している場合、登録ユーザーを強調表示することができます。

これは、あなたが複数著者によるブログ会員制サイトオンラインコースフォーラム、その他のタイプの複数ユーザーサイトを運営しているかに関わらず、価値ある実践方法です。ユーザーはあなたのサイトのスーパースターであり、彼らを認めることは健全なことなのです。

サイト上でユーザーや投稿者を強調する方法はたくさんあります。例えば、著者情報ボックスを追加し、最近登録されたユーザーを表示することができます。

しかし、人気のあるサイトでは、すべてのメンバーを1つのリストに含めることは難しいでしょう。サイトにアクセスするたびに、ランダムなユーザーを表示させたい場合もあるでしょう。

WordPressで登録ユーザーをランダムに表示する方法

ランダムユーザーのリストを表示するには、WordPressファイルにコードを追加する必要があります。この作業を行ったことがない場合は、WordPressでコードをコピー&ペーストする方法のガイドを参照してください。

WPCodeプラグインでコードを追加する

WordPressのチュートリアルにはサイト固有のプラグインで、テーマのfunctions.phpファイルにコードスニペットを追加する手順が書かれているのが一般的です。

しかし、問題は、カスタムコードスニペットにほんの小さなミスがあっただけでも、サイトの機能に支障をきたす可能性があるということです。例えば、コードに問題があると、サイトにアクセスできなくなります。

代わりに、WPCodeプラグインを使用することをお勧めします。WPCodeは使いやすく、初心者にも優しいプラグインで、WordPressサイトのどの部分にも安全にコードを追加することができます。

すべてのコードを一箇所にまとめ、テーマファイルを手動で編集した場合に発生するエラーを防ぎます。

つまり、WPCodeプラグインをインストールして有効化する必要があります。WordPressプラグインのインストール方法については、こちらをご覧ください。

有効化したら、‘Code Snippets’ >> ‘+‘Add Snippet‘.

Code snippets add snippet

スニペット・ライブラリが表示され、さまざまなコード・スニペットを使用できます。

しかし今回は、「カスタムコードを追加」をクリックして、独自のコードスニペットをサイトに追加します。

Add your custom code WPCode

スニペットのタイトルを’Random users’などとします。コードタイプ」を「PHPスニペット」に変更していることを本当に〜してもよいですか?

Random users code snippet

コード・プレビュー」ボックスに以下のコード・スニペットを追加する:

function wpb_random_users() {
    global $wpdb;

    if ( false === ( $usernames = get_transient( 'wpb_random_users_cache' ) ) ) {
        $usernames = $wpdb->get_results( "SELECT user_nicename, user_url, user_email FROM $wpdb->users ORDER BY RAND() LIMIT 5" );
        set_transient( 'wpb_random_users_cache', $usernames, REPLACE_WITH_SECONDS );
    }

    $randomusers = '<ul class="random-users">';

    // Display users in a list
    foreach ( $usernames as $username ) {
        if ( ! $username->user_url ) :
            $randomusers .= '<li>' . get_avatar( $username->user_email, 45 ) . $username->user_nicename . "</li>";
        else :
            $randomusers .= '<li>' . get_avatar( $username->user_email, 45 ) . '<a href="' . $username->user_url . '">' . $username->user_nicename . "</a></li>";
        endif;
    }
    $randomusers .= '</ul>';

    return $randomusers;
}

add_shortcode( 'randomusers', 'wpb_random_users' );

WPCodeではこうなります。

Randon users code preview

重要:’REPLACE_WITH_SECONDS’と書かれているテキストを、キャッシュを失効させたい秒数に置き換える必要があります。この数値を少なくとも300に設定することをお勧めします。この場合、キャッシュは5分(300秒)で失効します。それ以下に設定すると、WordPressサイトのスピードとパフォーマンスに深刻な影響を与える可能性があります。

これでランダムな5人のユーザーのリストが作成される。5行目の’LIMIT 5’の数字を変更することで、リスト内のユーザーを増やしたり減らしたりすることができます。

このコード・スニペットは、データベースのWordPressユーザー・テーブルにクエリーし、ランダムな行を選択します。そして、ユーザーのアバターと名前を箇条書きリストにした結果を出力します。ユーザーがプロフィールにサイトのURLを記入している場合は、ユーザー名もサイトにリンクされます。

コード・スニペットを追加したら、「Inactive」スイッチを「Active」に切り替えます。次に「Save Snippet」ボタンをクリックします。

Save custom snippet

保存したら、「インサーターメソッド」までスクロールダウンし、「ショートコード」をクリックする。WPCodeが生成したショートコードをコピーする。

Copy code snippet

次に、登録ユーザーのリストを表示するために、サイドメニューやその他のエリアにショートコードを追加する必要があります。ここでは2つの異なる方法を取り上げます。

投稿やウィジェットにランダム登録ユーザーを表示する

この方法は初心者にとって簡単です。以下のショートコードを使って、新規登録ユーザーを表示することができます:

[randomusers]

このショートコードを、ランダムな登録ユーザーのリストを表示したい投稿、ページ、またはウィジェットに貼り付けることができます。このチュートリアルでは、ウィジェットにリストを追加します。

外観 ” ウィジェットにナビゲーションし、「ウィジェット」の隣にある「プラス」ブロックインサーターアイコンをクリックする必要があります。

Plus block icon

ショートコード」ブロックを見つけて選択し、サイドバーやその他のウィジェットエリアにドラッグする必要があります。

Shortcode in widget

その後、WPCodeからコピーしたショートコードを’Shortcode’ボックスにペーストします。

更新」ボタンをクリックして変更を反映させることをお忘れなく。

Paste code snippet

投稿やページにショートコードを追加することで、同じ手順を踏むことができます。WordPressでカスタムコードを追加する方法についてのガイドを本当に〜してもよいですか?

これで、あなたのサイトにアクセスして、ランダムに登録されたユーザーのリストを表示することができます。どのように表示されるかを調整するには、サイトにカスタマイザーCSSを追加する必要があります。

Listing Random Registered Users in a Widget

WordPressテンプレートでランダム登録ユーザーを表示する

2つ目の方法は、テーマのテンプレートファイル(sidebar.phpやfooter.phpなど)にテンプレートタグを追加する方法です。

さらに詳しく知りたい方は、WordPressテンプレート階層チートシートをご覧ください。

次に、登録ユーザーのリストを表示したい場所に、以下のテンプレート・タグのコード・スニペットを追加します:

<?php wpb_random_users(); ?> 

WordPressで登録ユーザーをランダムに表示する方法について、このチュートリアルがお役に立てれば幸いです。また、無料のSSL証明書の取得方法や、専門家が選ぶ中小企業に最適なビジネス電話サービスもご覧ください。

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

情報開示 私たちのコンテンツは読者支援型です。これは、あなたが私たちのリンクの一部をクリックした場合、私たちはコミッションを得ることができることを意味します。 WPBeginnerの資金源 をご覧ください。3$編集プロセスをご覧ください。

アバター

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

究極のWordPressツールキット

ツールキットへの無料アクセス - すべてのプロフェッショナルが持つべきWordPress関連製品とリソースのコレクション!

Reader Interactions

17件のコメント返信を残す

  1. Syed Balkhi says

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. oleksandr says

    WPBEGINNER STAFF help plz
    i insert code, work fine.

    i saw “SELECT user_nicename, user_url, user_email

    How do make user_url – to go to profile ?

  3. Javier S says

    HI,

    Pretty nice code! Thank You!

    I am using Buddypress and bbPress, and I wonder how I can edit the code to use “user_nicename” with the url of profile and not with the website url?

    Thanks

  4. KIM says

    ah, Its working now!
    I pasted to the code at the function.php in current theme.
    is this correct, right?

    when I pasted the code to function.php in root directory, It was not working..

  5. WPBeginner Staff says

    This is called spam registrations. Check our guide on >a href=”https://www.wpbeginner.com/plugins/how-to-stop-spam-registrations-on-your-wordpress-membership-site/”>how to stop spam registrations in WordPress

  6. WPBeginner Staff says

    WordPress has a built in user management system. By default when you install WordPress you add the first user which is the administrator. You can add more users to your site by clicking on Users menu item in the admin bar.

    See: https://www.wpbeginner.com/glossary/user-role/

    However, user registration is turned off by default. So if you would like users to be able to register on your site, then you would go to Settings -> General and scroll down to the registration section. There you can check the box next to ‘any one can register’ to open your site for user registration.

    • Rachel Dreher says

      Thank you. I see that now. I do have it selected, and I’ve had a decent number of users added. I’m just not clear on how they are adding themselves.

  7. darkeizer says

    Hi I want to know if there is a plugin that can let me display a list of people (name, last name, address etc) that are registered in my organization. Not on my site.
    I have a access database with this info.
    better said, can i host a access file in wordpress?

  8. Beth says

    How do people register to become a registered user on my WordPress site? Many people register on my site but their emails do not appear to be genuine. Mostly the addresses look like gibberish and the username registered bears no resemblance to the email addresses. I have tried responding asking them to confirm their registration but there are no responses.

  9. Madeline says

    Hello,

    I know this is out of topic but how can I turn off a under construction page so the website is live to the public. Any suggestions? Looking forward a favorable answer. Thank you

    Madeline

    • WPBeginner Support says

      You can put a page back to draft. Edit the page and from the Publish Meta box, under the Status drop down menu, choose Draft. Your website will remain live but the page will become inaccessible. Once you are done with your changes you can publish the page.

      管理者

  10. Greg Whitehead says

    If you are to use this in your template files don’t you need to use an echo before the function call?

返信を残す

コメントありがとうございます。すべてのコメントは私たちのコメントポリシーに従ってモデレートされ、あなたのメールアドレスが公開されることはありませんのでご留意ください。名前欄にキーワードを使用しないでください。個人的で有意義な会話をしましょう。