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.

Observe the following code:

<ul>
<?php wpsc_start_category_query(array('category_group'=>get_option('wpsc_default_category'),'show_thumbnails'=>get_option('show_category_thumbnails'))); ?>
    <li>
    <div>
        <div class="image"><?php wpsc_print_category_image(); ?></div>
        <div class="caption-title transparent_class">
            <?php wpsc_print_category_name();?>
        </div>
        <div class="caption transparent_class">
            <a href="<?php wpsc_print_category_url();?>" class="wpsc_category_link"><?php wpsc_print_category_name();?></a>
            <?php if(get_option('wpsc_category_description')) :?>
            <?php wpsc_print_category_description("<div class='wpsc_subcategory'>", "</div>"); ?>
            <?php endif;?>
        </div>

    </div>
    </li>

    <?php wpsc_end_category_query(); ?>

</ul>

The above code displays all my product categories in a list. The problem with this approach is that I am not able to limit the number of categories that results. For example if I want to display 4 random categories, but have 6 in my WP database, then I won't get what I want.

Is there any alternate way to get the an x number of categories from all possiblities? Maybe with WP_Query?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

The wpsc_start_category_query() function basically calls the native WP get_terms() functions that is used to make a wp query:

$category_list = get_terms('wpsc_product_category','hide_empty=0&parent='.$category_id);

Since WPEC uses custom post types you can just build your own queries easily, here using http://codex.wordpress.org/Function_Reference/get_terms

But get_terms() doesn't do random, so you have to get them all and then shuffle the array into a random order, then output the first 4 (any number you set to $max) fo them. So adjusting your layout code it should be:

<ul>
<?php 
//display random sorted list of wpsc product categories
$counter = 0;
$max = 4; //number of categories to display
$terms = get_terms('wpsc_product_category');
shuffle ($terms); //makes list random
if ($terms) {
    foreach($terms as $term) {
        $counter++;
        if ($counter <= $max) { ?>
            <li>
            <div>
                <div class="image"><img src="<?php echo wpsc_category_image($term->term_id); ?>" width="<?php echo get_option('category_image_width'); ?>" height="<?php echo get_option('category_image_height'); ?>" /></div>
                <div class="caption-title transparent_class">
                    <?php echo $term->name; ?>
                </div>
                <div class="caption transparent_class">
                    <a href="<?php get_term_link( $term->slug, 'wpsc_product_category' ); ?>" class="wpsc_category_link"><?php echo $term->name; ?></a>
                    <?php if(get_option('wpsc_category_description')) :?>
                    <?php echo '<div class="wpsc_subcategory">'.$term->description.'</div>'; ?>
                    <?php endif;?>
                </div>

            </div>
            </li>
        <?php }
    }
}
?>
</ul>

I've tested the code and works like described.

share|improve this answer
    
Excellent. I would give you an upvote if I had enough reputation. –  jhc Apr 13 '13 at 15:23

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.