Your problem lies in this line of code in your first piece of code
'category' => $term->name,
There are two problems here, first of all, category
is not a valid parameter for WP_Query
, you should use category_name
, as you are working with get_terms
. get_terms
does not return the ID like get_categories
, which is an alternative for the first part of code. Secondly, If you use category_name
, remember, this is the slug of the category, not the category name
So replace
'category' => $term->name,
with
'category_name' => $term->slug,
As said, you can also use get_categories
, so you can do something like this as well
<?php $args = array (
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => true,
);
$categories = get_categories( $args );
foreach ( $categories as $category ) {
echo $category->name;
$post_args = array (
'cat' => $category->cat_ID,
'posts_per_page' => '1',
);
$query = new WP_Query( $post_args );
while ( $query->have_posts() ) {
$query->the_post(); ?>
<?php the_title(); ?>
<?php the_excerpt(); ?>
<?php }
wp_reset_postdata();
}
?>
Just to point out, you are passing a few invalid arguments to register_taxonomy
. Please go and check the codex for valid arguments
EDIT
From your comments, I think you are missing the closing endif;
To display the_post_thumbnail
, your code will look like this
if ( has_post_thumbnail() ) :
the_post_thumbnail();
endif;
Just one more thing I forgot to mention yesterday, you should always reset the postdata (wp_reset_postdata
)when you use a custom query