Выведем в любом месте сайта (через шорткод) нужное количество последних страниц (записей) родительской страницы (рубрики). В файле functions.php пропишем следующий код:
function wpschool_recent_posts_shortcode( $atts, $content = null ) {
global $post;
$post_id = get_the_ID();
$stati_children = new WP_Query(array(
'post_type' => 'page',
//'post_parent' => get_the_ID(),
'post_parent' => wp_get_post_parent_id($post_id),
'orderby' => 'rand',
//'order' => 'ASC',
'posts_per_page' => 3
)
);
$output ='<div class="pages-wrap">';
if($stati_children->have_posts()) :
while($stati_children->have_posts()): $stati_children->the_post();
$output .= '<ul><li><div class="last-pages-img"><a href="'. get_the_permalink() .'"><img src="'.get_the_post_thumbnail_url( get_the_id() ) .'"></a></div><p><a href="'. get_the_permalink() .'">'. get_the_title() .'</a></p></li></ul>';
endwhile;
endif; wp_reset_query();
$output .='</div>';
return $output;
}
add_shortcode( 'recent_posts', 'wpschool_recent_posts_shortcode' );
В строке ‘post_parent’ => wp_get_post_parent_id($post_id),
$post_id — это id текущей страницы (записи)
wp_get_post_parent_id() — wordpress функция, которая получает id родительской категории по отношению к определенной страницы (записи)
‘post_type’ => ‘page’, — указывает что мы выводим, page — страницы, post — записи
‘orderby’ => ‘rand’, — означает что выводить будем в случайном порядке
‘posts_per_page’ => 3 — сколько выводим страниц (записей)
Добавим немного стилей файл css:
.pages-wrap {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.pages-wrap ul {
max-width: 335px;
list-style: none;
margin-top: 10px;
}
.pages-wrap p{
margin-top: 20px;
font-family: Montserrat;
font-style: normal;
font-weight: 600;
font-size: 16px;
line-height: 140%;
}
.pages-wrap p a {
color: #3F484F;
}
Теперь, вставив такой шорткод [recent_posts] в любом месте сайта, получим примерно такой вид:















