Similar entries in the WordPress article. Plugins for displaying similar posts in WordPress

Greetings, dear ones! Few people know that, among other things, I also study web programming. This article opens a new section on our website dedicated to code. No, these are not lessons on any programming language, but simply useful code snippets that may be useful to you if you have your own website. For example, today we will talk about display similar posts to WordPress engine . Why this topic? Because I myself recently encountered this “problem” and want to help others.

Sometimes it happens that in the design theme WordPress opportunity display of similar posts is already present by default and everything is beautifully designed. However, most often this opportunity is not provided.

And we all know very well that “ similar posts"at the end of the article or on the sidebar has a very positive effect on behavioral factors(internal SEO optimization). And the first thing that comes to mind is to install a plugin that would display these very similar posts.

But there is always some kind of “BUT...”!

Firstly, any plugin is an additional load on the site and, as a result, slows it down a little. Therefore, experienced webmasters try to replace some plugins with clean code whenever possible.

Secondly, you can spend a day searching for a suitable plugin and setting it up, but in the end you won’t get the desired result. Yes, I also tried several of these plugins, and one of them even hung on my site for almost six months. But he didn’t look very good, to put it mildly (we won’t name names).

If you have even the slightest knowledge of the WordPress engine and styling (CSS), then the best solution will embed the code directly into your website template. Let's get started!

After a short search, I found such a ready-made code on the Internet, I was delighted, but my joy did not last long and now you will understand why.

Here is the code itself (click):

ID);
    if ($tags) ( $tag_ids = array(); foreach($tags as $individual_tag) $tag_ids = $individual_tag->term_id; $args=array("tag__in" => $tag_ids, "orderby"=>rand, "caller_get_posts"=>1, "post__not_in" => array($post->ID), "showposts"=>5); $my_query = new wp_query($args); if($my_query->have_posts()) ( echo "
  • "; while ($my_query->have_posts()) ( $my_query->the_post(); ?>!}">
  • " rel="bookmark" title="

For those who understand the code at least a little, it is clear what is used here php language, some standard WordPress features and it's all elegantly wrapped up in HTML markings We paste it in the right place (for many it is the single.php file in the theme root). And what do we get as a result? Here's what:

How "Related Posts" looks without thumbnails

Yes, this code showed us similar posts, but it showed them in a regular list and without thumbnails. Maybe this option will suit someone, but not me. We urgently need to do something about this.

First you need to get rid of the list. To do this, replace the tags “ ul" (unnumbered list) to tags " div" (simple block element) in two places ( ! ) and add a meaningful identifier to it (so that it can be conveniently styled later). Then we simply remove the tags of the list elements " li"(just leave their contents).

Inside the "div" you are now left with a bare link with the post title as the text. For convenience, I wrapped the title in another tag. And now the most important thing is to add a thumbnail to the posts. For this there is standard WordPress feature— the_post_thumbnail() , which returns us a thumbnail of the post in the “ tag img».

Now our code looks a little different. Our related posts are already displayed with thumbnails, and as you can see, we were able to achieve this without the help of third-party plugins. But I don’t recommend leaving everything like this. It still looks ugly. Huge pictures that span the entire page. Also in terrible quality. And under them are signatures in blue (regular links). You can paste this code and see everything with your own eyes. Although, insert it in any case, because we won’t get into this code anymore.

A little altered, compare (click):


ID);
if ($tags) (
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids = $individual_tag->term_id;
$args=array(
'tag__in' => $tag_ids,
‘orderby’=>rand,
'caller_get_posts'=>1,
‘post__not_in’ => array($post->ID),
‘showposts’=>5
);
$my_query = new wp_query($args);
if($my_query->have_posts()) (
echo '
’;
while ($my_query->have_posts()) (
$my_query->the_post();
?>
» rel=»bookmark» title=»»>



}
echo '
’;
}
wp_reset_query();
}
?>

Let's move on to point three or which one do we already have? Here I'll show you how I formatted my similar posts, but you might want to do it differently. The style code that I will provide below needs to be inserted into the styles file of your site (preferably at the end for convenience). It is located at the root of your site's theme and is called style.css (but it can also be in a different folder, for example, “css” or “styles”). Here's the code itself:

Style code (CSS):

#similar_posts(
margin: 30px 0;
}

#similar_posts h4(
margin-bottom: 15px;
}

#similar_posts_wrapper(
display: flex;
flex-wrap: nowrap;
justify-content: space-between;
}

#similar_posts_wrapper a(
flex-basis: 19%;
text-align: center;
text-decoration: none;
color: inherit;
border-radius: 5px;
transition: background-color 0.3s;
}

#similar_posts_wrapper a img(
padding: 3px;
border-radius: 5px;
}

#similar_posts_wrapper a:hover(
background-color: lightblue;
}

@media screen and (max-width: 767px)(
#similar_posts_wrapper(
flex-wrap: wrap;
justify-content: space-around;
}

#similar_posts_wrapper a(
flex-basis: 160px;
margin: 25px;
}
}

Stylized “Related Posts.” Do you feel the difference?

As a result, we got beautiful “similar posts” with thumbnails, which are also adaptive and with a barely noticeable smooth transition to the hover state (on mouse over). I hope everything worked out for you too.

Instead of the result:

Don't forget that the first code must be inserted into files with the .php extension. If you want to insert similar entries into the sidebar, then look for the file “ sidebar.php" But you won’t be able to simply insert this code into a sidebar widget.

Congratulations, you are now on your website similar posts with thumbnails, which you installed yourself without using plugins. And they most likely look exactly as you planned. And if not, then write in the comments what you can’t do, I’ll help in any way I can.

PS: At first, this article may not have similar entries, because we have just opened this section and, in fact, there are no other articles on the site with the same tags. And the code is guided by the tags that you write for your articles, so don’t forget to fill them out, this is important!

Did you read to the very end?

Was this article helpful?

Not really

What exactly did you not like? Was the article incomplete or false?
Write in comments and we promise to improve!

Good afternoon, dear readers!

Today we will make similar posts for a WordPress site without a plugin. This will be a continuation of the previous article.

In the last post I showed how to make similar posts using . It implements this functionality quite well and I recommend using it. But it can be replaced with very simple code that can also do the job quite well.

I am just writing this article when I checked this code on my blog. Everything works great. The block looks exactly the same.

As you can see, everything is similar to the plugin. And everything is done quite simply. You just need to place one part of the code in the place where the block of similar entries will be displayed.

However, I don’t know for sure whether pictures will be displayed if thumbnails for posts are not specified. I have all the thumbnails set, so I couldn’t test it on any article. Therefore, most likely, it will be necessary to assign thumbnails to each article. And, if there are a lot of them, then you will need to suffer a little. You also need your template to support thumbnails, because if this functionality is not present, then you will not be able to assign them to posts.

First, I give a video tutorial where I showed how to do everything, and then there is a text article with all the codes and explanations.

W is actually part of the code itself.