簡體   English   中英

如何僅顯示WordPress中自定義分類法的最新類別的帖子

[英]How to show only the posts from the most recent category of a custom taxonomy in WordPress

我有一個名為“問題”(如雜志問題)的自定義分類法,其類別以每個問題的標題命名。 我創建了一個名為“當前問題”的頁面,並在該站點的主導航中添加了指向該頁面的鏈接。

這是我現在在頁面模板中存在的循環:

$categories = get_terms('issue', 'orderby=count&order=asc');
     foreach( $categories as $category ): 
     ?>
     <h3><?php echo $category->name; ?></h3>
     <?php
     $posts = get_posts(array(
     'post_type' => 'issue_posts',
     'taxonomy' => $category->taxonomy,
     'term' => $category->slug,
     'nopaging' => true,
     ));
     foreach($posts as $post): 
     setup_postdata($post); 

它確實對類別和帖子進行了排序,但這會為所有類別拉入所有帖子。 我需要該鏈接僅顯示最近添加的類別。

最近添加的術語是ID最高的術語。 取一個按ID降序排列的術語,您將得到最近添加的術語。

$args = array(
    'number' => 1,
    'orderby' => 'ID',
    'order' => 'DESC'
);

$recent_issue = get_terms( 'issue', $args );

由於categories是Wordpress中的一種taxonomy ,因此沒有與之關聯的日期來告訴您“最新” categories是什么。 如果需要跟蹤最新創建的類別,則可以使用created_term ,它在Wordpress源中位於/wp-includes/taxonomy.php和函數wp_insert_term()

do_action("created_term", $term_id, $tt_id, $taxonomy);

類別的$taxonomy將是category (保存之前檢查),而$term_id將是您的類別ID。 從與該操作相關的函數中,可以調用update_option('latest_category_id', $term_id); 然后稍后使用get_option('latest_category_id');進行獲取get_option('latest_category_id'); 在您的get_posts()調用中使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM