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で同じ投稿者の関連投稿を表示する方法

編集メモ: WPBeginner のパートナーリンクから手数料を得ています。手数料は編集者の意見や評価に影響を与えません。編集プロセスについて詳しく知る。

WordPressで同じ投稿者の関連投稿を表示したいですか?

多くのサイトでは、記事の最後に関連投稿を表示している。しかし、複数著者のWordPressサイトを運営している場合、読者は同じ著者からの関連投稿を表示することを好むかもしれません。

今回は、WordPressで同じ投稿者の関連投稿を表示する方法を紹介する。

Displaying related posts by same author in WordPress

なぜWordPressで投稿者別に関連投稿を表示するのか?

WordPressで関連投稿を追加することで、訪問者が新しいコンテンツを見つけやすくなり、訪問者の興味を引きつけ、直帰率を下げながらページビューを増やすことができます。

しかし、複数著者のWordPressブログを運営している場合、訪問者は特定の著者の投稿をもっと読みたいと思うかもしれません。同じ著者が書いた投稿を表示すれば、訪問者を長くサイトにとどめ、読者体験を向上させることができる。

Related posts by author displayed manually in WordPress

ということで、WordPressで同じ投稿者の関連投稿を表示する方法を見てみよう。

WordPressで同じ投稿者の投稿を表示する(簡単な方法)

同じ投稿者による関連投稿のリストを表示する最も簡単な方法は、WordPressサイトにカスタムコードを追加することである。この目的のためにプラグインを探してみたが、見つかったものはあまりにも時代遅れだった。

サイトのfunctions.phpファイルを編集してカスタマイザーのコードを追加するようガイドに書かれていることがあります。しかし、コードの小さなミスやタイプミスがWordPressの一般的なエラーを引き起こしたり、サイトを完全に壊してしまう可能性があるため、この方法はお勧めしません。

そこでWPCodeの出番だ。

WPCodeは、WordPressサイトにカスタムのPHP、CSS、HTMLなどを安全に追加できる最高のコードスニペットプラグインです。また、カスタマイザーを失うことなくWordPressテーマを更新することができます。

まず、無料のWPCodeプラグインをインストールし、有効化する必要があります。詳しい手順は、WordPressプラグインのインストール方法についての初心者ガイドをご覧ください。

有効化したら、Code Snippets ” + Add Snippetにアクセスしてください。

Adding custom code to a WordPress blog or website

ここでは、サイトに追加できる既製のスニペットがすべて表示されます。これらのスニペットには、コメントを完全に無効化したり、WordPressが通常サポートしていないファイルタイプのアップロード、添付ファイルページの無効化などが含まれます。

スニペットを作成するには、「カスタムコードを追加」にマウスオーバーし、「スニペットを使用」を選択するだけです。

Displaying related posts using custom code and WPCode

カスタムスニペットの作成」ページが表示されますので、まずはコードタイプ名を入力してください。これは参考用なので、好きな名前を使ってください。

その後、’Code Type’ドロップダウンを開き、’PHP Snippet’を選択する。

How to display related posts by same author using WPCode

次のスニペットをコードエディターに貼り付けてください:

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サイト全体に表示されます。

Adding related posts across your WordPress website

その後、画面を一番上までスクロールし、「Inactive」トグルをクリックして「Active」に変更します。

最後に「Save Snippet」をクリックして、PHPスニペットをライブにする。

Clicking Save Snippet in WPCode

これで、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.

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

Avatar

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

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

  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. 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

      管理者

    • 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.

      管理者

    • 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

      管理者

  3. 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!

  4. 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.

    // Related Author Posts
    
    function wpb_related_author_posts($content) {
    
        global $authordata, $post;
    
        $authors_posts = get_posts( array( 'author' => $authordata->ID, 'post__not_in' => array( $post->ID ), 'posts_per_page' => 5 ) );
    
    if ( is_single() && (count($authors_posts) > 1 ) ) {
    
        $content .= '<div class="similar_posts"><h3>Similar Posts by The Author:</h3> ';
    
        $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></div>';
    
        return $content;
        }
        else {
        return $content;
        }
    }
    
    add_filter('the_content','wpb_related_author_posts');
    
  5. 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?

返信を残す

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