object(stdClass)#141 (9) {
["term_id"] => string(1) "3"
["name"] => string(9) "The Name of your Category"
["slug"] => string(9) "name-of-tax-term"
["term_group"] => string(1) "0"
["term_taxonomy_id"] => string(1) "3"
["taxonomy"] => string(11) "slug_of_tax"
["description"] => string(41) "Description of Term."
["parent"] => string(1) "0"
["count"] => string(1) "3"
}
因此,您希望获得类别slug,而不是其名称:
$cats = get_terms( \'category\' );
然后可以循环类别,如
foreach ( $cats as $cat ) {
$args = array(
\'category_name\' => $cat->slug,
\'paged\' => $paged,
\'posts_per_page\' => 2
);
// the loop
wp_reset_postdata();
}
如果循环在未更改的主查询上运行,则当前类别已存在于查询中。
只要换一下
$args = array(
\'category_name\' => $cat_name,
\'paged\' => $paged,
\'posts_per_page\' => 2
);
至
$args = array(
\'paged\' => $paged,
\'posts_per_page\' => 2
);
在代码中。
此外,不使用时get_the_category()
在循环中,必须传递一个post ID作为参数。