[英]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.