简体   繁体   中英

Display 3 posts per page order by published date DESC in Wordpress

I am displaying my custom posts 3 per page order by published date DESC. There are 8 posts and all I want is on page 1, it should display post 8 & 7, rather than 8,7,6. So the pagination should be:

page 1: 8,7

page 2: 6,5,4

page 3: 3,2,1

function get_paginated_links( $query ) {
    $currentPage = max( 1, get_query_var( 'paged', 1 ) );
    $pages = range( max( 1, $query->max_num_pages ), 1 );
    return array_map( function( $page ) use ( $currentPage ) {
        return ( object ) array(
            "isCurrent" => $page == $currentPage,
            "page" => $page,
            "url" => get_pagenum_link( $page )
        );
    }, $pages );
}

$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;

$posts_per_page = 3;

$the_query = new WP_Query(
        array(
            'posts_per_page' => $posts_per_page,
            'paged' => $paged,
            'orderby'=> 'publish_date',
            'order' => 'DESC'
        )
    );

foreach( get_paginated_links( $the_query ) as $index => $link ) :
echo the_content();
endwhile;

The above code shows post 8,7,6 on the first page. How do I fix it?

You can use post__in param to show only 8,7 for page 1. check the below code.

function get_paginated_links( $query ) {
    $currentPage = max( 1, get_query_var( 'paged', 1 ) );
    $pages = range( max( 1, $query->max_num_pages ), 1 );
    return array_map( function( $page ) use ( $currentPage ) {
        return ( object ) array(
            "isCurrent" => $page == $currentPage,
            "page"      => $page,
            "url"       => get_pagenum_link( $page )
        );
    }, $pages );
}

$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;

$posts_per_page = 3;

$args = array(
    'posts_per_page' => $posts_per_page,
    'paged'          => $paged,
    'orderby'        => 'publish_date',
    'order'          => 'DESC'
);

if( $paged == 1 ){
    $args['post__in'] = array( post_id_of_8, post_id_of_7 );
}

$the_query = new WP_Query( $args );

foreach( get_paginated_links( $the_query ) as $index => $link ) :
    echo the_content();
endwhile;

USEFUL LINKS

I ended up doing like this. It definitely can be improved. I will take this for now.

function get_paginated_links( $query ) {
    $currentPage = max( 1, get_query_var( 'paged', 1 ) );
    $pages = range( max( 1, $query->max_num_pages ), 1 );
    return array_map( function( $page ) use ( $currentPage ) {
        return ( object ) array(
            "isCurrent" => $page == $currentPage,
            "page"      => $page,
            "url"       => get_pagenum_link( $page )
        );
    }, $pages );
}

$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;

$posts_per_page = 3;

$the_query_temp = new WP_Query(
    array(
        'posts_per_page' => $posts_per_page,
        'orderby'=> 'publish_date',
        'order' => 'ASC'
    )
);
$max_num_pages = $the_query_temp->max_num_pages;
$reverse_paged = $max_num_pages + 1 - $paged;

$the_query_temp = new WP_Query(
    array(
        'posts_per_page' => $posts_per_page,
        'paged' => $reverse_paged,
        'orderby'=> 'publish_date',
        'order' => 'ASC'
    )
);

$posts_to_show = array();
if ($the_query_temp->have_posts() ): 
    while ($the_query_temp->have_posts() ): $the_query_temp->the_post();
        $posts_to_show[] = $post->ID;
    endwhile;
endif;
wp_reset_postdata(); 

$the_query = new WP_Query(
    array(
        'posts_per_page' => $posts_per_page,
        'post__in' => $posts_to_show,
        'orderby'=> 'publish_date',
        'order' => 'DESC'
    )
);


foreach( get_paginated_links( $the_query ) as $index => $link ) :
    echo the_content();
endwhile;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM