Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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>
share|improve this question

1 Answer 1

up vote 3 down vote accepted

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.

share|improve this answer
    
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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