I’m attempting to make a list of categories bound to a custom taxonomy, but using the featured image of the first post in each category.
Getting the categories was simple enough, but when I try to query for the post thumbnail I keep being presented with the details form the most recent post in the first category fetched.
I am running this outside The Loop, if that makes any difference;
<ul class="product-categories">
<?php
$categories = get_terms(
array(
'produkter'
),
array(
'hide_empty' => false,
)
);
foreach( $categories AS $cat )
{
$taxonomy = new WP_Query( array( 'posts_per_page' => 1, 'tax_query' => array( 'taxonomy' => 'produkter', 'terms' => $cat->slug ) ) );
while ( $taxonomy->have_posts() )
{
$taxonomy->the_post();
?>
<li class="produkter">
<div class="product-image">
<a href="https://wordpress.stackexchange.com/questions/95262/<?php bloginfo("wpurl' ); ?>/produkt/">
<?php the_post_thumbnail( "product-small" ); ?>
</a>
</div>
<a class="product-title" href="https://wordpress.stackexchange.com/questions/95262/<?php bloginfo("wpurl' ); ?>/produkt/"><?php echo $cat->name ?></a>
</li>
<?php
}
}
?>
</ul>