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>

1 Answer
1

tax_query takes an array of tax query arguments arrays (it takes an array of arrays) but you are using only single array. The correct code is as following.

<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, 'post_type' =>'book', 'tax_query' => array( array('taxonomy' => 'produkter','field' => 'slug', '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>

For More information visit this page.

Leave a Reply

Your email address will not be published. Required fields are marked *