WordPressで同じ投稿者の関連投稿を表示したいですか?
多くのサイトでは、記事の最後に関連投稿を表示している。しかし、複数著者のWordPressサイトを運営している場合、読者は同じ著者からの関連投稿を表示することを好むかもしれません。
今回は、WordPressで同じ投稿者の関連投稿を表示する方法を紹介する。
なぜWordPressで投稿者別に関連投稿を表示するのか?
WordPressで関連投稿を追加することで、訪問者が新しいコンテンツを見つけやすくなり、訪問者の興味を引きつけ、直帰率を下げながらページビューを増やすことができます。
しかし、複数著者のWordPressブログを運営している場合、訪問者は特定の著者の投稿をもっと読みたいと思うかもしれません。同じ著者が書いた投稿を表示すれば、訪問者を長くサイトにとどめ、読者体験を向上させることができる。
ということで、WordPressで同じ投稿者の関連投稿を表示する方法を見てみよう。
WordPressで同じ投稿者の投稿を表示する(簡単な方法)
同じ投稿者による関連投稿のリストを表示する最も簡単な方法は、WordPressサイトにカスタムコードを追加することである。この目的のためにプラグインを探してみたが、見つかったものはあまりにも時代遅れだった。
サイトのfunctions.phpファイルを編集してカスタマイザーのコードを追加するようガイドに書かれていることがあります。しかし、コードの小さなミスやタイプミスがWordPressの一般的なエラーを引き起こしたり、サイトを完全に壊してしまう可能性があるため、この方法はお勧めしません。
そこでWPCodeの出番だ。
WPCodeは、WordPressサイトにカスタムのPHP、CSS、HTMLなどを安全に追加できる最高のコードスニペットプラグインです。また、カスタマイザーを失うことなくWordPressテーマを更新することができます。
まず、無料のWPCodeプラグインをインストールし、有効化する必要があります。詳しい手順は、WordPressプラグインのインストール方法についての初心者ガイドをご覧ください。
有効化したら、Code Snippets ” + Add Snippetにアクセスしてください。
ここでは、サイトに追加できる既製のスニペットがすべて表示されます。これらのスニペットには、コメントを完全に無効化したり、WordPressが通常サポートしていないファイルタイプのアップロード、添付ファイルページの無効化などが含まれます。
スニペットを作成するには、「カスタムコードを追加」にマウスオーバーし、「スニペットを使用」を選択するだけです。
カスタムスニペットの作成」ページが表示されますので、まずはコードタイプ名を入力してください。これは参考用なので、好きな名前を使ってください。
その後、’Code Type’ドロップダウンを開き、’PHP Snippet’を選択する。
次のスニペットをコードエディターに貼り付けてください:
function wpb_related_author_posts($content) {
if (is_single()) {
global $authordata, $post;
// Fetch the author's display name
$author_name = get_the_author_meta('display_name', $authordata->ID);
// Insert the author's name into the string
$content .= '<h4>Similar Posts by ' . $author_name . ':</h4> ';
$authors_posts = get_posts(array(
'author' => $authordata->ID,
'post__not_in' => array($post->ID),
'posts_per_page' => 5
));
$content .= '<ul>';
foreach ($authors_posts as $authors_post) {
$content .= '<li><a href="' . get_permalink($authors_post->ID) . '">' . apply_filters('the_title', $authors_post->post_title, $authors_post->ID) . '</a></li>';
}
$content .= '</ul>';
return $content;
} else {
return $content;
}
}
add_filter('the_content', 'wpb_related_author_posts');
このコードはページが個別投稿かどうかをチェックし、もしそうなら投稿者の情報を取得する。
そして、投稿コンテンツの下に’Similar posts by (author name)’という見出しと、同じ投稿者による最大5つの類似投稿(現在の投稿を除く)を表示します。また、この関数は個別投稿テンプレート上でコードを実行するようWordPressに指示します。
その後、「インサーター」セクションまでスクロールダウンする。
すでに選択されていない場合は、「自動インサーター」を選択します。次に、ドロップダウンメニューを開き、「Run Everywhere」を選択すると、関連投稿がWordPressサイト全体に表示されます。
その後、画面を一番上までスクロールし、「Inactive」トグルをクリックして「Active」に変更します。
最後に「Save Snippet」をクリックして、PHPスニペットをライブにする。
これで、WordPressブログの投稿日にアクセスすると、新しい関連投稿セクションが表示されます。
WordPressで同じ投稿者の関連投稿を簡単に表示する方法について、この投稿がお役に立てれば幸いです。ブログのトラフィックを増やす方法についてのガイド、またはWordPressの検索エンジン最適化(SEO)プラグインとツールのエキスパートピックもご覧ください。
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 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!
L Waterfield says
Apologies for opening this up again after all these years, but could you tell me how to place this related posts content somewhere other than “after entry content”? E.g. If I wanted it to appear after the author info or after the comments section? Thank you!
WPBeginner Support says
If you wanted to customize the specific location it appears then you would need to change ‘the_content’ to where you would like to add the content
管理者
azad says
How can Display Related Product by the Same Author in Wp sidebar
WPBeginner Support says
You would want to reach out to the support for the ecommerce plugin you are using for how to set that up.
管理者
Alex says
Great, Thank you for your article!
is it possible to get same data from CPT?
WPBeginner Support says
To do that you would want to add to the array of arguments to include the post type. For line 8 you would want it to be:
$authors_posts = get_posts( array( ‘post_type’ => ‘book’, ‘author’ => $authordata->ID, ‘post__not_in’ => array( $post->ID ), ‘posts_per_page’ => 5 ) );
Replace book with your custom post type
管理者
Alex says
Great! thank you!
WPBeginner Support says
You’re welcome
Ryan Caswell says
Hi there, the code works great but is there any way to include the post featured image thumb too? This would be so amazing!
WPBeginner Support says
Hi Ryan,
Yes, you can add
the_post_thumbnail();
to display featured image.管理者
Ryan Caswell says
Great thanks! Sorry I am not much of PHP developer. Where would it go into the code to have it display? Thanks so much!
Trishah Woolley says
Yes that works. Thanks!
And I found and fixed another issue… If the author only has one post the Related Posts area shows but there is no information in it. To solve this, I did the following. And I also added a div around the content in order to style the area.
1-click Use in WordPress
Trishah Woolley says
I’m testing the above functions.php code on a development site. The related posts are showing up on the bottom of pages also, like the contact us page. As you are using is_single this shouldn’t be happening. Do you have any insight on why this is happening?
WPBeginner Support says
Hi Trishah,
Thanks for reporting this. There was a tiny error in the code that caused this. We have fixed the error, you can now try the new code snippet.
管理者