背景与要求:在该term术语分类下文章按照时间升序排序,需要在这个分类页面上精准获取到第一篇文章。
注意:是要应用在taxonomy.php或者taxonomy-*.php主题模板中。
WordPress中term分类术语页面展示其中的第一篇文章的实现函数:
<?php
$zzw_args_curterm = array(
'post_type' => 'any', // post类型:自定义post类型(post type slug)、post
'post_status' => array('publish'), // 发布状态: pending, draft, auto-draft, future, private, inherit, trash, any
'posts_per_page' => '1', // 显示数量
'order' => 'ASC', // 或者: ASC
'orderby' => 'date', // 排序依据: none, rand, id, title, slug, modified, parent, menu_order, comment_count
'tax_query' => array(
array(
'taxonomy' => get_queried_object()->taxonomy,
'field' => 'slug',
'terms' => get_queried_object()->slug,
),
),
);
$zzw_query_curterm = new WP_Query($zzw_args_curterm);
if ($zzw_query_curterm->have_posts()) {
while ($zzw_query_curterm->have_posts()) {
$zzw_query_curterm->the_post()
?>
<p><?php echo esc_html(get_queried_object()->name); ?>分类的第一篇文章:<a href="<?php the_permalink(); ?>"><?php the_title();?></a></p>
<?php
}
}
wp_reset_postdata();
?>

