I have a feeling that I'm on the right track, I just don't have enough solid PHP knowledge to get much further than where I'm currently at.
I'm currently using the following code to return a list of child categories from a single category:
<?php
$taxonomyName = "category";
$terms = get_terms($taxonomyName,array('parent' => 79));
echo '<ul>';
foreach($terms as $term) {
echo '<li>';
echo '<a href="'.get_term_link($term->slug,$taxonomyName).'">'.$term->name.'</a><br/>';
$thumbnails = get_posts('numberposts=1&orderby=rand');
foreach ($thumbnails as $thumbnail) {
echo '<a href="' . get_permalink( $thumbnail->ID ) . '" title="' . esc_attr( $thumbnail->post_title ) . '">';
if ( has_post_thumbnail($thumbnail->ID)) {
echo get_the_post_thumbnail($thumbnail->ID, 'thumbnail');
} else {
echo 'no thumbnail';
}
echo '</a>';
}
echo '<li>';
}
echo '</ul>';
?>
This code works somewhat. It returns a list of all six sub categories under the parent, ID 79. However, I want to also return one random thumbnail in each of the list items for each of the 6 sub categories.
Unfortunately, this code returns a random thumbnail from all of my posts, not just ID 79 and it's specific child. I need it to return one thumbnail from the same category that is returned in it's parent <li>
.
Is there some easy way to do this? I'm thinking I need to sort through that array and return the category in the nested foreach loop. I just don't know how to do that.
Thanks in advance for any help!