I've got the following code to show all categories and the last post in a category - using Wordpress:
foreach( $categories as $category ) {
$category_link = sprintf(
'<a href="%1$s" alt="%2$s">%3$s</a>',
esc_url( get_category_link( $category->term_id ) ),
esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ),
$category->name ) ),
esc_html( $category->name )
);
global $post;
$args = array( 'posts_per_page' => 1, 'category' => $category->term_id);
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
$newpost="<a href=".get_the_permalink()."><b>".get_the_title()."</b></a>
$newdate=get_the_date();
endforeach;
Right now the categories are sorted by alphabet.
Is there a way to sort them by the date of the last post?
I thought about putting an argument to the first line like:
foreach( $categories as $category sort by ... )
But I couldn't find a way.
Maybe you can help?
Thank you for your help!
Just found this working code: get_categories order by last post
You can sort records of $categories like :
$categories = array('post_status'=>'publish','post_type' => 'test', 'orderby' =>
'date', 'order' => 'DESC','paged' => $paged );
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.