<?php
/*
Plugin Name: Текст
Description: Текст
Author: Текст
*/
add_filter( 'the_content', 'wfm_related_posts' );
function wfm_related_posts($content){
if( !is_single() ) return $content;
$id = get_the_ID();
$categories = get_the_category( $id );
foreach($categories as $category){
$cats_id[] = $category->cat_ID;
}
$related_posts = new WP_Query(
'orderby' => 'title',
'order' => ASC,
'posts_per_page' => 4,
'category__in' => $cats_id,
'post__not_in' => array($id) )
);
if( $related_posts->have_posts() ){
while( $related_posts->have_posts() ){
$related_posts->the_post();
$content .= '<a href="' . get_permalink() . '">' . get_the_title() . '</a><br>';
}
$content .= '</div>';
wp_reset_query();
}
return $content;
}
PD9waHAKLyoKUGx1Z2luIE5hbWU6INCi0LXQutGB0YIKRGVzY3JpcHRpb246INCi0LXQutGB0YIKQXV0aG9yOiDQotC10LrRgdGCCiovCgphZGRfZmlsdGVyKCAndGhlX2NvbnRlbnQnLCAnd2ZtX3JlbGF0ZWRfcG9zdHMnICk7CgoKZnVuY3Rpb24gd2ZtX3JlbGF0ZWRfcG9zdHMoJGNvbnRlbnQpewoKCWlmKCAhaXNfc2luZ2xlKCkgKSByZXR1cm4gJGNvbnRlbnQ7CgoJJGlkID0gZ2V0X3RoZV9JRCgpOwoJJGNhdGVnb3JpZXMgPSBnZXRfdGhlX2NhdGVnb3J5KCAkaWQgKTsKCglmb3JlYWNoKCRjYXRlZ29yaWVzIGFzICRjYXRlZ29yeSl7CgkJJGNhdHNfaWRbXSA9ICRjYXRlZ29yeS0+Y2F0X0lEOwoJfQoJCgkkcmVsYXRlZF9wb3N0cyA9IG5ldyBXUF9RdWVyeSgKCQlhcnJheSgKCQkJJ29yZGVyYnknID0+ICd0aXRsZScsCgkJCSdvcmRlcicgPT4gQVNDLAoJCQkncG9zdHNfcGVyX3BhZ2UnID0+IDQsCgkJCSdjYXRlZ29yeV9faW4nID0+ICRjYXRzX2lkLAoJCQkncG9zdF9fbm90X2luJyA9PiBhcnJheSgkaWQpCgkJKQoJKTsKCglpZiggJHJlbGF0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