3

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="<?php bloginfo( 'wpurl' ); ?>/produkt/">
                                <?php the_post_thumbnail( "product-small" ); ?>
                            </a>
                        </div>

                        <a class="product-title" href="<?php bloginfo( 'wpurl' ); ?>/produkt/"><?php echo $cat->name ?></a>
                    </li>

                <?php
                }
            }
    ?>
</ul>
3

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="<?php bloginfo( 'wpurl' ); ?>/produkt/">
                            <?php the_post_thumbnail( "product-small" ); ?>
                        </a>
                    </div>

                    <a class="product-title" href="<?php bloginfo( 'wpurl' ); ?>/produkt/"><?php echo $cat->name ?></a>
                </li>

            <?php
            }
        }
?>
</ul>

For More information visit this page.

  • Thank you, that seems to have done the trick, I was oblivious to the 2nd array thinking it was a typo because it didn't make much sense to double-array the tax_query section. – Clorith Apr 10 '13 at 13:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.