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

Jak wyświetlić powiązane wpisy tego samego autora w WordPress?

Jeśli prowadzisz blog z wieloma autorami, taki jak nasz, to wiesz, jak ważne jest skuteczne prezentowanie pracy każdego z nich.

Jednym ze sposobów jest wyświetlanie powiązanych postów tego samego autora na końcu każdego artykułu. Takie podejście nie tylko pomaga czytelnikom znaleźć więcej tego, co lubią, ale także zapewnia każdemu autorowi większą widoczność w Twojej witrynie.

W tym artykule pokażemy, jak wyświetlać powiązane posty tego samego autora w WordPress.

Displaying related posts by same author in WordPress

Dlaczego warto wyświetlać wpisy powiązane według autora w WordPress?

Dodając powiązane wpisy w WordPressie, możesz pomóc odwiedzającym znaleźć nowe treści, utrzymać ich zaangażowanie i zwiększyć liczbę odsłon, jednocześnie zmniejszając współczynnik odrzuceń.

Jeśli jednak prowadzisz blog WordPress z wieloma autorami, odwiedzający mogą chcieć przeczytać więcej wpisów konkretnego autora. Jeśli pokażesz wpisy napisane przez tego samego autora, możesz zatrzymać odwiedzających na twojej witrynie na dłużej i poprawić wrażenia czytelników.

Related posts by author displayed manually in WordPress

To powiedziawszy, zobaczmy, jak można wyświetlić powiązane wpisy tego samego autora w WordPress.

Wyświetlanie wpisów tego samego autora w WordPress (prosta metoda)

Najprostszym sposobem na wyświetlenie listy powiązanych wpisów tego samego autora jest dodanie własnego kodu do twojej witryny WordPress. Próbowaliśmy znaleźć wtyczki do tego celu, ale te, które znaleźliśmy, są zbyt nieaktualne.

Czasami przewodniki mówią, aby dodać własny kod poprzez edycję pliku functions.php twojej witryny. Nie zalecamy jednak tej metody, ponieważ nawet niewielki błąd lub literówka w kodzie może spowodować typowe błędy WordPressa lub nawet całkowicie zepsuć twoją witrynę.

I tu z pomocą przychodzi WPCode.

WPCode's homepage

WPCode to najlepsza wtyczka do fragmentów kodu, która pozwala bezpiecznie dodawać niestandardowe pliki PHP, CSS, HTML i inne do witryny WordPress. Możesz także zaktualizować swój motyw WordPress bez utraty dostosowań.

Oprócz wyświetlania powiązanych postów tego samego autora, WPCode pozwala dodawać niestandardowy kod, aby wyświetlać losowe posty, wykluczać lepkie posty z pętli postów i nie tylko.

Najpierw należy zainstalować i włączyć darmową wtyczkę WPCode. Aby uzyskać więcej instrukcji, zapoznaj się z naszym przewodnikiem dla początkujących, jak zainstalować wtyczkę WordPress.

Po włączaniu, przejdź do Code Snippets ” + Add Snippet z panelu bocznego administratora WordPress. Tutaj zobaczysz wszystkie gotowe fragmenty kodu, które możesz dodać do swojej witryny internetowej.

Aby utworzyć fragment kodu, wystarczy najechać kursorem na „Add Your Custom Code (New Snippet)”, a następnie kliknąć przycisk „+ Add Custom Snippet”.

Adding Custom Code in WPCode

Następnie należy wybrać rodzaj kodu z listy opcji, które pojawią się na ekranie. W przypadku tego poradnika należy wybrać „Fragment kodu PHP”.

Select PHP Snippet as the code type

Następnie zostaniesz przeniesiony na stronę „Create Custom Snippet”, gdzie możesz zacząć od wpisania nazwy twojego fragmentu kodu.

Nazwa jest tylko dla twojego odniesienia, więc możesz użyć czegokolwiek chcesz.

How to display related posts by same author using WPCode

Możesz teraz wkleić poniższy fragment kodu w polu „Podgląd kodu”:

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');

Ten kod sprawdzi, czy strona jest pojedynczym wpisem, a jeśli tak, to pobierze informacje o autorze.

Następnie wyświetli nagłówek „Podobne wpisy autora (nazwa autora)” i do 5 podobnych wpisów (z wyłączeniem obecnego, aktualnego) tego samego autora poniżej treści wpisu. Funkcja nakazuje również WordPressowi wykonanie kodu na szablonach pojedynczych wpisów.

Następnie przewiń w dół do sekcji „Wstaw”.

Jeśli nie jest jeszcze zaznaczona, wybierz opcję „Automatycznie wstaw”. Następnie otwórz menu rozwijane i wybierz „Uruchom wszędzie”, aby powiązane wpisy pojawiały się w całej twojej witrynie internetowej WordPress.

Adding related posts across your WordPress website

Następnie możesz przewinąć ekran do góry i kliknąć przełącznik „Nieaktywne”, aby zmienił się na „Aktywne”.

Na koniec kliknij „Zapisz fragment”, aby uruchomić fragment kodu PHP.

Clicking Save Snippet in WPCode

Teraz, jeśli przejdziesz na dowolny wpis na twoim blogu WordPress, zobaczysz nową sekcję powiązanych wpisów.

Mamy nadzieję, że ten artykuł pomógł ci dowiedzieć się, jak łatwo wyświetlać powiązane posty tego samego autora w WordPress. Możesz również zapoznać się z naszym przewodnikiem na temat wyświetlania popularnych postów według wyświetleń w WordPressie lub naszymi typami ekspertów dla najlepszych wtyczek powiązanych postów dla WordPressa.

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

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