Question

I tried posting this on the WordPress exchange, but unfortunately I didn't get any further along, so I'm trying it here. Hopefully this works!

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>.

Additionally, the code returns "no thumbnail" if there is no code, or if I take the else out, it returns nothing. I'd like to make it to where it returns at least one image every time, so ideally there would be some kind of logic that says to always return at least one image. I just don't know how to do that.

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, but unfortunately it's over my head.

I think I'm looking for something similar to this person, but unfortunately, they didn't get any replies. -> https://stackoverflow.com/questions/18750040/random-featured-image-based-on-category

Thanks in advance for any help!

Was it helpful?

Solution

So in order to do this, I needed to first do a for each loop, store the category slug as a variable, JAMterm, and then use that in a query to pull one random thumbnail from the category.

Thanks to @Renishkhunt for helping me along the way to get this answer.

<?php
    $taxonomyName = "category";
    $terms = get_terms($taxonomyName,array('parent' => 79));
    echo '<ul>';
    foreach($terms as $term) {
        echo '<li><a href="'.get_term_link($term->slug,$taxonomyName).'">'.$term->name.'</a><br/>';

        $JAMterm = $term->slug;

        global $wp_query;
        $term = $wp_query->queried_object;

        $args=array(
            'orderby' => 'rand',
            'posts_per_page' => 1,
            'post_type' => 'post',
            'tax_query' => array(
                array(
                    'taxonomy' => 'category', 
                    'field' => 'slug', 
                    'terms' =>  $JAMterm
                )
            )
        );

        $new_query = null;
        $new_query = new WP_Query($args);

        while ($new_query->have_posts()) : $new_query->the_post();
            the_post_thumbnail(); 
        endwhile;
        wp_reset_postdata();


        echo '</li>';
        }
    echo '</ul>';
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top