2017-07-28 58 views
0

我有下面的代碼來顯示所有類別和帖子的最後一個類別 - 使用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

回答

-2

您可以排序的$記錄類別,如:

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

這樣你只需要做循環,因爲你的變量將遞減已經過篩選,按日期 –

+0

我不明白downvote,因爲這個答案看起來像我站在哪裏的最佳解決方案,考慮問題是多麼模糊。但是,該評論本應附加在問題本身之後。 – ksjohn

+0

你有沒有試過我的解決方案? –