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

Hur man visar Relaterade inlägg av samma författare i WordPress

Om du driver en blogg med flera författare som vår, vet du hur viktigt det är att visa upp varje författares arbete på ett effektivt sätt.

Ett sätt som vi har hittat är att visa relaterade inlägg av samma författare i slutet av varje artikel. Detta tillvägagångssätt hjälper inte bara läsarna att hitta mer av det de gillar, utan ger också varje författare mer synlighet på din webbplats.

I den här artikeln visar vi dig hur du visar relaterade inlägg av samma författare i WordPress.

Displaying related posts by same author in WordPress

Varför visa Relaterade inlägg efter författare i WordPress?

Genom att lägga till relaterade inlägg i WordPress kan du hjälpa besökare att hitta nytt content, hålla dem engagerade och öka sidvisningar samtidigt som du minskar avvisningsfrekvensen.

Men om du run en WordPress blogg med flera författare, kanske besökare vill läsa fler post av en specifik författare. Om du visar poster som skrivits av samma författare kan du hålla besökarna kvar på din site längre och förbättra läsarupplevelsen.

Related posts by author displayed manually in WordPress

Med det sagt, låt oss se hur du kan visa relaterade inlägg av samma författare i WordPress.

Displaying Posts by the Same Author in WordPress (Enkel metod)

Det enklaste sättet att visa en lista över relaterade inlägg av samma författare är genom att lägga till customize-kod till din WordPress-webbplats. Vi har försökt hitta tillägg för detta ändamål, men de som vi hittade är alldeles för outdated.

Ibland kommer guider att säga till dig att add custom code genom att editera din sites functions.php-fil. Vi rekommenderar dock inte den här metoden eftersom även ett litet misstag eller skrivfel i koden kan orsaka vanliga WordPress error eller till och med förstöra din site helt.

Det är där WPCode kommer in i bilden.

WPCode's homepage

WPCode är det bästa kodsnuttspluginet som gör att du säkert kan lägga till anpassad PHP, CSS, HTML och mer till din WordPress-webbplats. Du kan också uppdatera ditt WordPress-tema utan att förlora din anpassning.

Förutom att visa relaterade inlägg av samma författare kan du med WPCode lägga till anpassad kod för att visa slumpmässiga inlägg, utesluta klistriga inlägg från inläggsslingan och mycket mer.

Först måste du installera och aktivera det kostnadsfria WPCode-pluginet. För mer instruktioner, se vår nybörjarguide om hur du installerar ett WordPress-plugin.

Efter aktivering går du till Code Snippets ” + Add Snippet från WordPress admin sidofält. Här ser du alla färdiga kodsnuttar som du kan lägga till på din webbplats.

För att skapa ett snippet håller du bara muspekaren över ”Add Your Custom Code (New Snippet)” och klickar sedan på knappen ”+ Add Custom Snippet”.

Adding Custom Code in WPCode

Därefter måste du välja en kodtyp från listan med alternativ som visas på skärmen. För denna handledning väljer du ”PHP Snippet”.

Select PHP Snippet as the code type

Du kommer då till sidan ”Create Custom Snippet”, där du kan börja med att skriva ett namn på ditt kodavsnitt.

Namnet är bara för referens, så du kan använda vad du vill.

How to display related posts by same author using WPCode

Du kan nu gå vidare och klistra in följande kodsnutt i rutan ”Code Preview”:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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');

Den här koden kontrollerar om sidan är ett enskilt inlägg och hämtar i så fall information om författaren.

Sedan visas en heading som säger ”Similar posts by (author name)” och upp till 5 liknande posts (exklusive det aktuella inlägget) av samma författare under postens content. Funktionen säger också till WordPress att exekvera koden på templates för enskilda inlägg.

Därefter rullar du ner till ”Insertion” section.

Om den inte redan är vald väljer du ”Auto Insert”. Öppna sedan dropdown-menyn och välj ”Run Everywhere” så att de relaterade inläggen visas på hela din WordPress website.

Adding related posts across your WordPress website

Efter det är du redo att rulla högst upp på vyn och klicka på ”Inaktiverad” toggle så att den ändras till ”Aktiv”.

Slutligen klickar du på ”Save Snippet” för att göra PHP snippet live.

Clicking Save Snippet in WPCode

Nu, om du besöker något inlägg på din WordPress blogg, kommer du att se en new related posts section.

Vi hoppas att den här artikeln hjälpte dig att lära dig hur du enkelt kan visa relaterade inlägg av samma författare i WordPress. Du kanske också vill se vår guide om hur du visar populära inlägg efter visningar i WordPress eller våra expertval för de bästa relaterade inläggs-pluginsen för WordPress.

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.

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us. Here's our editorial process.

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.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit - a collection of WordPress related products and resources that every professional should have!

Reader Interactions

14 kommentarerLeave a Reply

  1. L Waterfield

    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

      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

      Admin

  2. azad

    How can Display Related Product by the Same Author in Wp sidebar

    • WPBeginner Support

      You would want to reach out to the support for the ecommerce plugin you are using for how to set that up.

      Admin

  3. Alex

    Great, Thank you for your article!
    is it possible to get same data from CPT?

    • WPBeginner Support

      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

      Admin

      • Alex

        Great! thank you!

        • WPBeginner Support

          You’re welcome :)

  4. Ryan Caswell

    Hi there, the code works great but is there any way to include the post featured image thumb too? This would be so amazing!

      • Ryan Caswell

        Great thanks! Sorry I am not much of PHP developer. Where would it go into the code to have it display? Thanks so much!

  5. Trishah Woolley

    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
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    // 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');
  6. Trishah Woolley

    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

      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.

      Admin

Leave A Reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.