从codex for get_posts()
category参数需要是类别的ID,而不是类别名称。
所以你应该category_name
参数,或者您可以将ID传递给category
.
<?php
$term_name = get_queried_object()->name;
$args = array ( \'category_name\' => $term_name, \'posts_per_page\' => 5);
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post);
?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach; ?>
或
<?php
$term_id = get_queried_object()->term_id;
$args = array ( \'category\' => $term_id, \'posts_per_page\' => 5);
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post);
?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach; ?>