【问题标题】:Wordpress: $categories as $category - sort by dateWordpress:$categories as $category - 按日期排序
【发布时间】:2017-07-28 13:16:38
【问题描述】:

我有以下代码来显示所有类别和类别中的最后一篇文章 - 使用 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; 

现在这些类别是按字母排序的。

有没有办法按照上一篇文章的日期对它们进行排序?

我想过在第一行添加一个参数,例如:

 foreach( $categories as $category sort by ... )

但我找不到办法。

也许你能帮忙?

感谢您的帮助!

刚刚找到这个工作代码: get_categories order by last post

【问题讨论】:

    标签: php wordpress sorting categories


    【解决方案1】:

    您可以对 $categories 的记录进行排序,例如:

    $categories = array('post_status'=>'publish','post_type' => 'test', 'orderby' => 
    'date', 'order' => 'DESC','paged' => $paged );
    

    【讨论】:

    • 这样你只需要循环,因为你的变量已经按日期降序过滤了
    • 我不理解反对票,因为考虑到问题的含糊程度,从我的立场来看,这个答案看起来是最好的解决方案。但是,该评论应该附加到问题本身。
    • 我为什么要试试你的解决方案?
    • 我不明白这段代码,更不用说它是如何解决问题的了。为什么将 get_posts 的参数放在 $categories 中?
    • 感谢您的回复。我刚刚找到了这个:stackoverflow.com/questions/43100515/… 为我工作!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-14
    • 2014-06-12
    • 2018-11-23
    • 1970-01-01
    • 1970-01-01
    • 2013-06-05
    • 1970-01-01
    相关资源
    最近更新 更多