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

Wie man: Verwandte Beiträge mit Thumbnails in WordPress ohne Plugins

Hinweis der Redaktion: Wir erhalten eine Provision für Partnerlinks auf WPBeginner. Die Provisionen haben keinen Einfluss auf die Meinung oder Bewertung unserer Redakteure. Erfahre mehr über Redaktioneller Prozess.

Möchten Sie eine Liste verwandter Beiträge auf Ihrer WordPress-Website anzeigen und lieber Code als ein Plugin verwenden?

Wenn die Besucher Ihres Blogs einen Artikel, der sie interessiert, zu Ende gelesen haben, können Sie ihnen eine Liste mit verwandten Beiträgen anbieten, um sie bei der Stange zu halten und ihnen zu helfen, neue Inhalte zu lesen.

In diesem Artikel zeigen wir Ihnen, wie Sie mit WordPress verwandte Beiträge mit Code anzeigen können, ohne dass ein Plugin erforderlich ist.

How to: Related Posts with Thumbnails in WordPress Without Plugins

Warum verwandte Seiten in WordPress anzeigen?

Wenn Ihr WordPress-Blog zu wachsen beginnt, kann es für die Benutzer schwieriger werden, andere Beiträge zum selben Thema zu finden.

Die Anzeige einer Liste verwandter Inhalte am Ende jedes Blogbeitrags ist eine gute Möglichkeit, Ihre Besucher auf Ihrer Website zu halten und die Seitenaufrufe zu erhöhen. Es hilft auch, die Sichtbarkeit Ihrer wichtigsten Seiten zu verbessern, indem Ihre besten Inhalte dort angezeigt werden, wo die Besucher sie leicht finden können.

Wenn Sie mit Code nicht vertraut sind, ist es einfacher, eines der vielen WordPress-Plugins für verwandte Beiträge zu wählen, die verwandte Beiträge ohne Code anzeigen können.

Wenn Sie sich aber schon immer gefragt haben, ob Sie verwandte Beiträge auch ohne ein Plugin anzeigen können, stellen wir Ihnen zwei verschiedene Algorithmen vor, mit denen Sie verwandte Beiträge mit Thumbnails allein mit Code generieren können:

Hinweis: Wenn Sie zu jedem verwandten Beitrag ein Thumbnail anzeigen möchten, müssen Sie diesen Beiträgen zunächst ein Bild hinzufügen.

Methode 1: Wie man verwandte Beiträge in WordPress nach Tags anzeigt

Eine effiziente Möglichkeit, verwandte Inhalte zu finden, besteht darin, nach anderen Beiträgen zu suchen, die dieselben Tags aufweisen. Tags werden oft verwendet, um sich auf die spezifischen Details in einem Beitrag zu konzentrieren.

Daher sollten Sie den Beiträgen, die Sie miteinander in Verbindung bringen möchten, einige gemeinsame Tags hinzufügen. Sie können sie in das Feld „Tags“ im WordPress-Editor eingeben.

The ‘Tags’ Settings Box in the WordPress Editor

Nachdem Sie Ihren Beiträgen Tags hinzugefügt haben, müssen Sie als Nächstes den folgenden Codeschnipsel in die Vorlage single.php Ihres Themes einfügen. Wenn Sie Hilfe beim Hinzufügen von Code zu Ihrer Website benötigen, lesen Sie unsere Anleitung zum Einfügen von Snippets aus dem Internet in WordPress.

$orig_post = $post;
global $post;
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
$args=array(
'tag__in' => $tag_ids,
'post__not_in' => array($post->ID),
'posts_per_page'=>5, // Number of related posts that will be shown.
'ignore_sticky_posts'=>1
);
$my_query = new wp_query( $args );
if( $my_query->have_posts() ) {
   
echo '<div id="relatedposts"><h3>Related Posts</h3><ul>';
   
while( $my_query->have_posts() ) {
$my_query->the_post(); ?>
   
<li><div class="relatedthumb"><a href="<?php the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><!--?php the_post_thumbnail(); ?--></a></div>
<div class="relatedcontent">
<h3><a href="<?php the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><!--?php the_title(); ?--></a></h3>
<!--?php the_time('M j, Y') ?-->
</div>
</li>
<!--?php }
echo '</ul--></ul></div>';
}
}
$post = $orig_post;
wp_reset_query();

Dieser Code sucht nach Tags, die mit einer Seite verbunden sind, und führt dann eine Datenbankabfrage aus, um Seiten mit ähnlichen Tags zu finden.

Wo sollten Sie den Code platzieren? Das hängt von Ihrem Theme ab, aber in den meisten Fällen sollten Sie den Code in die Vorlage single.php Ihres Themes nach dem Hauptbeitrag und direkt über dem Kommentarbereich einfügen können.

Wenn Sie das Twenty Twenty-One-Theme verwenden, wie wir es auf unserer Demoseite tun, dann ist ein guter Ort, um den Code in die Datei template-parts/content/content-single.php nach dem Header und direkt nach <?php the_content(); einzufügen.

Related Content by Tags Preview

Dadurch werden verwandte Inhalte automatisch in jedem WordPress-Beitrag angezeigt. Sie müssen das Styling und das Aussehen Ihrer verwandten Beiträge ändern, um sie an Ihr Theme anzupassen, indem Sie benutzerdefiniertes CSS hinzufügen.

Related Posts example

Tipp: Anstatt die Dateien Ihres Themes zu bearbeiten, was Ihre Website zerstören könnte, empfehlen wir die Verwendung eines Code-Snippets-Plugins wie WPCode.

WPCode macht es sicher und einfach, benutzerdefinierten Code in WordPress hinzuzufügen. Außerdem bietet es „Einfügeoptionen“, mit denen Sie automatisch Snippets an bestimmten Stellen Ihrer WordPress-Website einfügen und ausführen können, z. B. nach einem Beitrag.

WPCode insertion options for custom code snippets

Weitere Details finden Sie in unserem Leitfaden zum Hinzufügen von benutzerdefiniertem Code in WordPress. Sie können auch unseren ausführlichen WPCode-Test lesen, um mehr über das Plugin zu erfahren.

Methode 2: Verwandte Beiträge in WordPress nach Kategorie anzeigen

Eine weitere Möglichkeit, verwandte Inhalte anzuzeigen, ist die Auflistung von Beiträgen, die sich in derselben Kategorie befinden. Der Vorteil dieser Methode ist, dass die Liste der verwandten Beiträge fast nie leer ist.

Wie bei Methode 1 müssen Sie einen Code-Snippet in die Vorlage single.php Ihres Themes oder in ein Code-Snippets-Plugin wie WPCode einfügen. Weitere Einzelheiten finden Sie unter Methode 1 und in unserer Anleitung zum Hinzufügen von benutzerdefiniertem Code in WordPress.

$orig_post = $post;
global $post;
$categories = get_the_category($post->ID);
if ($categories) {
$category_ids = array();
foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
$args=array(
'category__in' => $category_ids,
'post__not_in' => array($post->ID),
'posts_per_page'=> 2, // Number of related posts that will be shown.
'ignore_sticky_posts'=>1
);
$my_query = new wp_query( $args );
if( $my_query->have_posts() ) {
echo '<div id="related_posts"><h3>Related Posts</h3><ul>';
while( $my_query->have_posts() ) {
$my_query->the_post();?>
   
<li><div class="relatedthumb"><a href="<?php the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><!--?php the_post_thumbnail(); ?--></a></div>
<div class="relatedcontent">
<h3><a href="<?php the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><!--?php the_title(); ?--></a></h3>
<!--?php the_time('M j, Y') ?-->
</div>
</li>
<!--?php }
echo '</ul--></ul></div>';
}
}
$post = $orig_post;
wp_reset_query();

Jetzt sehen Sie eine Liste mit verwandten Inhalten am Ende jedes Beitrags.

Wenn Sie die Gestaltung und das Erscheinungsbild Ihrer verwandten Seiten ändern möchten, müssen Sie benutzerdefinierte CSS hinzufügen, die zu Ihrem Thema passen.

Wir hoffen, dass dieses Tutorial Ihnen geholfen hat zu lernen, wie man verwandte Beiträge mit Miniaturansichten in WordPress ohne Plugins anzeigt. Vielleicht möchten Sie auch erfahren, wie Sie die Besucher Ihrer WordPress-Website verfolgen können, oder unsere Liste mit 24 Tipps zur Beschleunigung Ihrer Website lesen.

Wenn Ihnen dieser Artikel gefallen hat, dann abonnieren Sie bitte unseren YouTube-Kanal für WordPress-Videotutorials. Sie können uns auch auf Twitter und Facebook finden.

Offenlegung: Unsere Inhalte werden von unseren Lesern unterstützt. Das bedeutet, dass wir möglicherweise eine Provision verdienen, wenn Sie auf einige unserer Links klicken. Mehr dazu erfahren Sie unter Wie WPBeginner finanziert wird , warum das wichtig ist und wie Sie uns unterstützen können. Hier finden Sie unseren redaktionellen Prozess .

Das ultimative WordPress Toolkit

Erhalte KOSTENLOSEN Zugang zu unserem Toolkit - eine Sammlung von WordPress-bezogenen Produkten und Ressourcen, die jeder Profi haben sollte!

Reader Interactions

183 KommentareEine Antwort hinterlassen

  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. Abhijit Badgujar says

    Hi,

    I have a ‚Related post‘ option from my wordpress theme and i am already using it. I have displayed 6 posts after the content. The problem is, it only shows 3 related posts and next three can be seen when you slide it horizontally. I don’t want that option, i want the site to display all 6 posts outright. Can you tell me how to do it?

  3. Gyuricza Laszlo says

    Hello,

    Your guidance was amazing but how can i exclude a specific category from the related posts in order to not display it?

    Thank you, in advance!
    Sincerely,
    LAszlo Gyuricza

  4. Tom says

    Nice solution but not definitive for my exigence. Infact the mean problem is that this code sort the related posts from the most recent in the same category or tags. The result is that when you browse inside a category/tag you’ll always display the same few last posts, limiting hardly the older posts of your site. That’s a right conclusion? Please if you tried it share your opinion!

    • Joy says

      1. After ‚caller_get_posts’=>1 put a comma(,)
      2. Hit enter button [next line]
      3. Add ‚orderby’=>’rand‘
      You are done. Now related posts will be shown randomly. Thank you.

  5. Zane DeVault says

    This code works great. I was wondering if you could explain what this part of the code does?

    $orig_post = $post;
    global $post;

    $post = $orig_post;

    I think I have a grasp on what the rest is doing, but this is throwing me for a loop.

    Thanks for all your great content!

  6. atiq says

    in Twentyfifteen default theme where should i insert this code in single.php file? If i inserted above the endwhile; it shows syntax error, unexpected ‘endwhile’ and if i inserted below the endwhile; but above the endif; it shows syntax error, unexpected ‘endif’

    Any solution for this?

    Thanks

  7. atiq says

    in Twentyfifteen default theme where should i insert this code in single.php file? If i inserted above the enwhile; it shows syntax error, unexpected ‚endwhile‘ and if i inserted below the enwwile but above the endif; it shows syntax error, unexpected ‚endif‘

    Any solution for this?

    Thanks

  8. Marcel Tripoux says

    Hi! Great post!

    Is there a way to combine both option, in order to call related tags only in the current category ?

  9. Bambang says

    my single.php layout :

    //the_content bla bla bla code here

    //Copy paste Related Posts by Tags code here

    //comments_template bla bla bla code here

    ——————————-
    the result i got error :
    Parse error: syntax error, unexpected ‚endwhile‘ (T_ENDWHILE) in …
    ——————————-
    after i change “ <? } " to " <?php } " it worked,

    just sugestion, maybe it better if you put complete php open tag
    thanks :)

  10. Alex says

    Works perfect. How to exclude the definite tag from Related Posts by Tags? I mean how to change the code when algorithm would find other posts with any one of the tag (except tag 595 for instance) that the current post has and will list them.

  11. Mohammad says

    Thanks for the great code
    It works great but you didn’t address any css codes for a more beautiful look for this section. Can you please do this? I’m newbie to coding and I tried some codes but they didn’t work. In your codes there is:
    echo ‚Related Posts‘;
    but in some similar codes I found in other resources there is:

    and in css some codes like this:
    .relatedposts {
    font-size: 12px;
    width: 640px;
    }
    .relatedposts h3 {
    font-size: 20px;
    margin: 0 0 5px;
    }
    will get that a nice look but it didn’t work with your code.
    Thanks

  12. Muthu says

    Dear collegue this is an error am getting while pasting this code on single.php file.kindly tell me exacctly where should i paste this code.

    Parse error: syntax error, unexpected end of file in C:\xampp\htdocs\beingusefull\wp-content\themes\TechPlus\single.php on line 78

  13. WPBeginner Staff says

    That will depend on your individual theme and template. You need to add the conditional tag after the WordPress loop begins. After this line:
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    • Jenny says

      I got that error too and this is my updated code:
      ID);
      if ($tags) {
      $tag_ids = array();
      foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
      $args=array(
      ‚tag__in‘ => $tag_ids,
      ‚post__not_in‘ => array($post->ID),
      ‚posts_per_page’=>5, // Number of related posts that will be shown.
      ‚caller_get_posts’=>1
      );
      $my_query = new wp_query( $args );
      if( $my_query->have_posts() ) { ?>
      Related Posts
      have_posts() ) : $my_query->the_post(); ?>
      <a href="“ rel=“bookmark“ title=““>

      <a href="“ rel=“bookmark“ title=““>

    • Lacey Tech Solutions says

      If you’re using Yoast’s WordPress SEO plugin you can specify the thumbnail URL under the social tab for the post. If the social tab isn’t shown then you need to click the Yoast SEO link in the WordPress admin menu then click „Social“. In the social page tick the option for, „Add Open Graph meta data“ and save. Go back into your post and you can specify the thumbnail image you want to use when the post is shared. If you don’t implicitly set the post image the user has the option of selecting any image that appears on the page, which is why your recent post images are being pulled in when you share the article link.

  14. Rachael says

    This doesn’t work in the single.php for me, b/c related posts show up at the very bottom of the page. It works with loop.php but then they also show up on the homepage – any ideas on a fix so it just shows in single posts and not the homepage?

  15. Mason Coulter says

    Is there a way to add pagination to the related posts query? I cant seem to get pagination to work on a secondary query within single.php. Thanks!

  16. Tom K. says

    Hello , i want to ask , is there way to make , that related posts would be by category and tags in one place? Thanks.

  17. Jonathan says

    Is there a way to choose a single category (let’s call it Brands) and then have it display related posts only affiliated with the child categories under Brands? So, the hierarchy for the cats would be Brands > JCPenny. I want to only show related posts for JCPenny. But, that child category could be different per post. So if a post uses a different child category it’ll show related posts for that child cat. Can this code be modified to handle that somehow?

  18. Miro says

    Hi, thanks for the code, but instead of grabbing the featured image as a thumb, can i grab instead the first image in my posts? Thanks

  19. Caleb says

    Great post thanks! I run a website that uses WP more as a CMS with a large number of pages rather than posts. Can I do this with as related pages instead, so that it’s grabbing related pages and not posts? If so how do I go about doing this.

    Thanks for the help :)

  20. Phil Simon says

    This. Is. Awesome. WPEngine understandably doesn’t allow for related posts plugins, save for a few exceptions. I tried a few and really didn’t like them. I threw this code into single.php and voila! Thank you.

  21. Lucas Bishop says

    This is good, but i wanna do it with title of post rather then tags or category. Any suggestions for it, syed!!

  22. Alexandros says

    Hi friends little problem it appears to me: „Parse error: syntax error, unexpected ‚endwhile‘ (T_ENDWHILE) in C: \ xampp \ htdocs \ z1 \ wp-content \ themes \ mytheme \ single.php on line 65“ find
    But what ..? Thanks

  23. Rakesh says

    Take a look at the category related posts code. It wont work unless you add some php , the three letters before getting started with echo. Add php and it will not be ignored. In either case, it is ignored and wordpress cries every time with an error note. Thanks for the code by the way and it made my work a lot easier. Thanks a ton for that and do adjust it ,

  24. Dennis says

    Also, I have one more question,

    What if I wanted to display a certain post containing a certain tag to all posts, how would I do this?

  25. Dennis says

    Hi, this is so awesome! Now I can just get rid of my related post plugin, so thanks A LOT for that!

    One question I have is, how can I display the related post in 2 column?

    Once again, this has been such a BIG help for me!

  26. Christina says

    Hi!! Thanks for your helpful information for those, like myself, who have no idea of any coding, but still want to have a blog!! I have just incorporated your ‚relate posts by tags“ code to my single post file. It works except for the pfotos. They dont show at all and the titles of my related posts are shown the one under the other, like a list. What have I done wrong?? Thanks in advance for your answer!!

  27. TheFran says

    Hi,

    This code is amazing! Thank you so much. Is there a way I can make the code show related posts from ANOTHER SITE? So it searches the posts from another site that I show below the post as a related post on my site?

    Thanks!

  28. Carlos says

    Hi,
    I’m using a theme that works with custom posts. I wonder if I try your code, which part should I edit to display just those custom posts instead of the regular posts.

    Thanks

  29. Miz.Chellie says

    Hello – yours is the first tutorial that I found that works but I would like the list vertical. Can you give an example of the CSS for that?

  30. Livius says

    Hey,

    I cannot, find the single.php where I have to insert this code.

    The problem is that I am using Balance Theme + Genesis. So in the genesis, I can find the Single.php, but it says to „Not Edit Under Any Circumstances“, and I have actually pasted the code in there, but it made my site to stop opening pages at all.
    And in the Child Balance Theme, there is no Single.php ….

    Any suggestions?

    • Editorial Staff says

      Yes, don’t edit the framework file. You would have to familiarize yourself with Genesis hooks and filters. Then add the code in one of the loop hooks for single page using your functions.php file. Unfortunately, due to the amount of frameworks out there, we can’t possibly cover our tips for all frameworks.

      Admin

  31. Dean says

    Hi! this is very useful coding. Is it possible to use this for making a custom page that will show all the posts, like a sitemap but with thumbnails? Something like this:

    Category 1
    —– related posts code (all posts from that category) ——

    Category 2
    —– related posts code (all posts from that category) ——

    etc. It would make a great showcase for categories with few posts. Thank you for reading and help!

  32. Nuno Marques says

    Hey,

    very handy your „Related Posts by Category“.

    Sometimes I believe it’s better to have add a raw code rather than use a plugin doesn’t is so expansible…

    Thank you!

  33. Paul says

    Thank you thank you thank you!! A simple copy paste bit of code that just gets on with it and works – does exactly what it says. This is exactly what I was looking for :)

  34. tobalseverin says

    hi!

    i need some help…

    How i can filter.. the category, but if i have parent and child categories and only i show the child post. ex:

    – product (all product, this is the parent) (id 104)

    – KindOfProducts (subcategory, this is the child) (id 109)

    – KindOfProductsTwo (subcategory, this is the child) (id 110)

    in products have all post but just need show related from the child: KindOfProducts.

    i try whit this:

    $args = array(

    ‚category__in‘ => $category_ids,

    ‚category__not_in‘ => 104,

    ‚post__not_in‘ => array($post->ID),

    ‚orderby’=> ‚rand‘,

    ’showposts‘ => 100,

    ‚ignore_sticky_posts‘ => 1

    );

    but dont showme nothing…

    and i try whit this other one:

    $args = array(

    ‚category__in‘ => $category_ids,

    ‚child_of‘ => 104,

    ‚post__not_in‘ => array($post->ID),

    ‚orderby’=> ‚rand‘,

    ’showposts‘ => 100,

    ‚ignore_sticky_posts‘ => 1

    );

    and nothing

    help? tnks!

Eine Antwort hinterlassen

Danke, dass du einen Kommentar hinterlassen möchtest. Bitte beachte, dass alle Kommentare nach unseren kommentarpolitik moderiert werden und deine E-Mail-Adresse NICHT veröffentlicht wird. Bitte verwende KEINE Schlüsselwörter im Namensfeld. Lass uns ein persönliches und sinnvolles Gespräch führen.