Answer the question
In order to leave comments, you need to log in
How to display the last 3 posts on a wordpress page?
Good time everyone.
There are several pages in wordpress. Home, Blog, Contacts, About us.
In the reading settings, it is worth showing the latest posts. How to make it so that only the last 3 posts are shown on the main page, and everything in the "Blog". At the same time, as I understand it, all 4 use 1 and the same template. So, immediately a condition, on the remaining 2 pages, the last posts should not be shown.
Answer the question
In order to leave comments, you need to log in
paste in functions.php
/**
* для ограничения вывода записей на главной
*/
function cr_home_count_posts( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'posts_per_page', 3 );
}
}
add_action( 'pre_get_posts', 'cr_home_count_posts' );
/**
* редирект со страниц пагинации на главной
*/
function cr_home_only_first_page() {
if ( is_home() && is_paged() ) {
wp_redirect( get_home_url(),301);
exit;
}
}
add_action( 'template_redirect', 'cr_home_only_first_page' );
function cr_remove_navigation_markup_template( $template, $class ) {
if ( is_home() ) $template='';
return $template;
};
add_filter( 'navigation_markup_template', 'cr_remove_navigation_markup_template', 10, 2 );
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question