Two ways to display similar records using wordpress code

How to display related posts in WordPress without plugins

Sorting function similar items by tag (tags)

Many use tags or labels, which serve for tying articles, but the entries can be in different categories. Insert this code in the single.php file under Article.

Council! Templates are different, experiment and if you do not succeed the first time to insert the code in the desired location, then just try to insert it into another. But do not forget that the right to keep the, then to, suddenly, restore everything in place.

01<div class="similar_records">
02<h3>Похожие записи:</h3>
03<?php $tags = wp_get_post_tags($post->ID);
04if ($tags) {
05 $tag_ids = array();
06 foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
07 $args=array(
08 'tag__in' => $tag_ids, // Сортировка производится по тегам
09 'post__not_in' => array($post->ID),
10 'showposts'=>5 // Цифра означает количество выводимых записей
11 );
12 $my_query = new wp_query($args);
13 if( $my_query->have_posts() ) {
14 echo '<ul>';
15 while ($my_query->have_posts()) {
16 $my_query->the_post();
17 ?>
18 <li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
19 <?php
20 }
21 echo '</ul>';
22 }
23 wp_reset_query();
24 }
25 ?></div>

If the code does not change, the output of similar articles will be implemented by Tag, but taking into account the date, starting with themselves fresh. To become displayed in a random order, you must add the line:

'orderby'=>rand,

prescribes the following 9 lines:

'tag__in' => $tag_ids,

Also, do not be amiss to add 1 condition:

'caller_get_posts'=>1,

It forbids the repetition of the same reference. for example, on your blog is still a small number of articles, and you have specified in the code of the output 10 reference:

'showposts'=>10,

In this instance, in the same block references may appear similar records. I hope it is clear.

As a result, we have to get a code:

01<div class="similar_records">
02<h3>Похожие записи:</h3>
03<?php $tags = wp_get_post_tags($post->ID);
04if ($tags) {
05 $tag_ids = array();
06 foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
07 $args=array(
08 'tag__in' => $tag_ids, // Сортировка происходит по тегам (меткам)
09 'orderby'=>rand, // Добавляем условие сортировки рандом (случайный подбор)
10 'caller_get_posts'=>1, // Запрещаем повторение ссылок
11 'post__not_in' => array($post->ID),
12 'showposts'=>5 // Цифра означает количество выводимых записей
13 );
14 $my_query = new wp_query($args);
15 if( $my_query->have_posts() ) {
16 echo '<ul>';
17 while ($my_query->have_posts()) {
18 $my_query->the_post();
19 ?>
20 <li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
21 <?php
22 }
23 echo '</ul>';
24 }
25 wp_reset_query();
26 }
27 ?></div>

Now conclude similar records will be made in random order.

Sorting function similar items by category (category)

Personally, I use a second method of deriving similar records, as I think, that sorting by category most suitable for text captions, But in any case the choice is yours.

01<div class="similar_records">
02<h3>Похожие записи:</h3>
03<?php $categories = get_the_category($post->ID);
04if ($categories) {
05 $category_ids = array();
06 foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
07 $args=array(
08 'category__in' => $category_ids, // Сортировка производится по категориям
09 'orderby'=>rand, // Условие сортировки рандом
10 'post__not_in' => array($post->ID),
11 'showposts'=>5, //Количество выводимых записей
12 'caller_get_posts'=>1); // Запрещаем повторение ссылок
13 $my_query = new wp_query($args);
14 if( $my_query->have_posts() ) {
15echo '<ul>';
16 while ($my_query->have_posts()) {
17 $my_query->the_post();
18 ?>
19 <li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
20 <?php
21 }
22 echo '</ul>';
23 }
24wp_reset_query();
25}
26?></div>

I immediately turned on the sorting condition rand, but if you want to set the sort by date, simply remove the line:

'orderby'=>rand,

I remind, the code to be inserted into the file single.php after the article. Usually it is inserted right after the scripts of social buttons, but if you have difficulty, please ask questions in the comments.

A source: https://seo-mayak.com/

Rate article
IT News
Add a comment