RE: «Похожие записи» без плагина на WordPress

Возможно ли сделать "Похожие записи" (в конце записей) без плагина на WordPress, как это делает популярный плагин "Related  Posts"?

junona Начальный Asked on 10.12.2014 in Хаки.
Add Comment
2 Answers

СПОСОБ 1 - Похожие записи с миниатюрами (релевантность диктуется тегами к публикациям).

 <div class="relatedposts">
 <h3>Похожие записи</h3>
 <?php
 $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'=>7, // количество записей
 'caller_get_posts'=>1
 );
$my_query = new wp_query( $args );
 while( $my_query->have_posts() ) {
 $my_query->the_post();
 ?>
<div class="relatedthumb">
 <a rel="external" href="<? the_permalink()?>"><?php the_post_thumbnail(array(150,100)); ?><br />
 <?php the_title(); ?>
 </a></div>
 <? }
 }
 $post = $orig_post;
 wp_reset_query();
 ?></div>
 

Стили:

 .relatedthumb {margin: 0 1px 0 1px; float: left; }
 .relatedthumb img {margin: 0 0 3px 0; padding: 0;}
 .relatedthumb a {color :#333; text-decoration: none; display:block; padding: 4px; width: 120px;}
 .relatedthumb a:hover {background-color: #ddd; color: #000;}
 .relatedposts {margin: 0 0 20px 0; float: left; font-size: 12px;}
 .relatedposts h3 {font-size: 20px; margin: 0 0 5px 0; }
 

Результат выполнения:

RE:

СПОСОБ 2 - Похожие записи без миниатюр (релевантность диктуется тегами к публикациям).

 <div class="relatedthumb">
 <h3>Похожие записи:</h3>
 <ul>
 <?php
 $tags = wp_get_post_tags($post->ID);
 if ($tags) {
 $first_tag = $tags[0]->term_id;
 $args=array(
 'tag__in' => array($first_tag),
 'post__not_in' => array($post->ID),
 'showposts'=>5,
 'caller_get_posts'=>1
 );
 $my_query = new WP_Query($args);
 if( $my_query->have_posts() ) {
 while ($my_query->have_posts()) : $my_query->the_post(); ?>
 <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Читать <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
 <?php
 endwhile;
 }
 wp_reset_query();
 }
 ?>
 </ul>
 </div>
 

Стили:

 .relatedthumb  {
 background: #2FBFC0;
 border: 1px solid #EC3233;
 border-radius: 8px;
 color: white;
 font-family: "Georgia";
 font-size: 14px;
 font-style: italic;
 margin-bottom: 10px;
 padding: 10px;
 }
 

Результат выполнения:

RE:

 

СПОСОБ 3 - Похожие записи с миниатюрами (выводятся сообщения из той же рубрики). Очень полезный метод (модификация способа №1), если у материалов отсутствуют теги (метки) по которым нельзя вывести релевантные материалы.

<div class="relatedposts">
<h3>Похожие записи:</h3>
<?php
$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), //Не выводить текущую запись
 'showposts'=>5, // Указываем сколько похожих записей выводить
 'caller_get_posts'=>1
 );
 $my_query = new wp_query($args);
 if( $my_query->have_posts() ) {
while ($my_query->have_posts()) {
 $my_query->the_post();
?>
<div class="relatedthumb">
 <a rel="external" href="<?the_permalink()?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail(array(140,100)); ?><br />
 <?php the_title(); ?>
 </a>
 </div>
<?php
}
}
wp_reset_query();
}
?>
</div>

Стили:

 .relatedthumb {margin: 0 1px 0 1px; float: left; }
 .relatedthumb img {margin: 0 0 3px 0; padding: 0;}
 .relatedthumb a {color :#333; text-decoration: none; display:block; padding: 4px; width: 120px;}
 .relatedthumb a:hover {background-color: #ddd; color: #000;}
 .relatedposts {margin: 0 0 20px 0; float: left; font-size: 12px;}
 .relatedposts h3 {font-size: 20px; margin: 0 0 5px 0; }
 

ГУРУ Answered on 10.12.2014.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.