WordPressサイトで登録ユーザーを紹介したいですか?
ユーザーを強調することで、コミュニティ意識を醸成し、会員数を増やすことができます。しかし、多くのメンバーがいるサイトでは、数人のユーザーをランダムに表示するだけでも意味があります。
この投稿では、WordPressサイトにランダムな登録ユーザーのリストを表示する方法を紹介します。
WordPressで登録ユーザーをランダムに表示する理由とは?
ユーザー登録が可能なWordPressサイトを運営している場合、登録ユーザーを強調表示することができます。
これは、あなたが複数著者によるブログ、会員制サイト、オンラインコース、フォーラム、その他のタイプの複数ユーザーサイトを運営しているかに関わらず、価値ある実践方法です。ユーザーはあなたのサイトのスーパースターであり、彼らを認めることは健全なことなのです。
サイト上でユーザーや投稿者を強調する方法はたくさんあります。例えば、著者情報ボックスを追加し、最近登録されたユーザーを表示することができます。
しかし、人気のあるサイトでは、すべてのメンバーを1つのリストに含めることは難しいでしょう。サイトにアクセスするたびに、ランダムなユーザーを表示させたい場合もあるでしょう。
WordPressで登録ユーザーをランダムに表示する方法
ランダムユーザーのリストを表示するには、WordPressファイルにコードを追加する必要があります。この作業を行ったことがない場合は、WordPressでコードをコピー&ペーストする方法のガイドを参照してください。
WPCodeプラグインでコードを追加する
WordPressのチュートリアルには、サイト固有のプラグインで、テーマのfunctions.phpファイルにコードスニペットを追加する手順が書かれているのが一般的です。
しかし、問題は、カスタムコードスニペットにほんの小さなミスがあっただけでも、サイトの機能に支障をきたす可能性があるということです。例えば、コードに問題があると、サイトにアクセスできなくなります。
代わりに、WPCodeプラグインを使用することをお勧めします。WPCodeは使いやすく、初心者にも優しいプラグインで、WordPressサイトのどの部分にも安全にコードを追加することができます。
すべてのコードを一箇所にまとめ、テーマファイルを手動で編集した場合に発生するエラーを防ぎます。
つまり、WPCodeプラグインをインストールして有効化する必要があります。WordPressプラグインのインストール方法については、こちらをご覧ください。
有効化したら、‘Code Snippets’ >> ‘+‘Add Snippet‘.
スニペット・ライブラリが表示され、さまざまなコード・スニペットを使用できます。
しかし今回は、「カスタムコードを追加」をクリックして、独自のコードスニペットをサイトに追加します。
スニペットのタイトルを’Random users’などとします。コードタイプ」を「PHPスニペット」に変更していることを本当に〜してもよいですか?
コード・プレビュー」ボックスに以下のコード・スニペットを追加する:
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ではこうなります。
重要:’REPLACE_WITH_SECONDS’と書かれているテキストを、キャッシュを失効させたい秒数に置き換える必要があります。この数値を少なくとも300に設定することをお勧めします。この場合、キャッシュは5分(300秒)で失効します。それ以下に設定すると、WordPressサイトのスピードとパフォーマンスに深刻な影響を与える可能性があります。
これでランダムな5人のユーザーのリストが作成される。5行目の’LIMIT 5’の数字を変更することで、リスト内のユーザーを増やしたり減らしたりすることができます。
このコード・スニペットは、データベースのWordPressユーザー・テーブルにクエリーし、ランダムな行を選択します。そして、ユーザーのアバターと名前を箇条書きリストにした結果を出力します。ユーザーがプロフィールにサイトのURLを記入している場合は、ユーザー名もサイトにリンクされます。
コード・スニペットを追加したら、「Inactive」スイッチを「Active」に切り替えます。次に「Save Snippet」ボタンをクリックします。
保存したら、「インサーターメソッド」までスクロールダウンし、「ショートコード」をクリックする。WPCodeが生成したショートコードをコピーする。
次に、登録ユーザーのリストを表示するために、サイドメニューやその他のエリアにショートコードを追加する必要があります。ここでは2つの異なる方法を取り上げます。
投稿やウィジェットにランダム登録ユーザーを表示する
この方法は初心者にとって簡単です。以下のショートコードを使って、新規登録ユーザーを表示することができます:
[randomusers]
このショートコードを、ランダムな登録ユーザーのリストを表示したい投稿、ページ、またはウィジェットに貼り付けることができます。このチュートリアルでは、ウィジェットにリストを追加します。
外観 ” ウィジェットにナビゲーションし、「ウィジェット」の隣にある「プラス」ブロックインサーターアイコンをクリックする必要があります。
ショートコード」ブロックを見つけて選択し、サイドバーやその他のウィジェットエリアにドラッグする必要があります。
その後、WPCodeからコピーしたショートコードを’Shortcode’ボックスにペーストします。
更新」ボタンをクリックして変更を反映させることをお忘れなく。
投稿やページにショートコードを追加することで、同じ手順を踏むことができます。WordPressでカスタムコードを追加する方法についてのガイドを本当に〜してもよいですか?
これで、あなたのサイトにアクセスして、ランダムに登録されたユーザーのリストを表示することができます。どのように表示されるかを調整するには、サイトにカスタマイザーCSSを追加する必要があります。
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.
Syed Balkhi
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!
oleksandr
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 ?
Javier S
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
KIM
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..
WPBeginner Staff
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
WPBeginner Staff
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
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.
darkeizer
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?
Beth
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.
WPBeginner Support
Please check out our article on how to stop spam registrations on your WordPress site.
管理者
Rachel Dreher
I have the same question. Where are those people registering? I don’t see a button or link on my site to be able to do that. Sorry if its a stupid question!
Madeline
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
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.
管理者
Madeline Yau
Would this help google search?
Madeline
WPBeginner Support
Probably not.
管理者
Greg Whitehead
If you are to use this in your template files don’t you need to use an echo before the function call?
WPBeginner Support
No.
管理者