I
I
IvanKalinin2016-01-10 09:47:11
WordPress
IvanKalinin, 2016-01-10 09:47:11

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

1 answer(s)
W
WP Panda, 2016-01-10
@IvanKalinin

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' );

With disabling pagination, it's more fun if everything is strictly in the theme of the API and is used for output, the_posts_pagination () available from 4.1, then do this
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 );

If pagination is not displayed through the_posts_pagination() , then everything is individual and you need to look

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question