List of subcategories with featured image

How can I add the featured image?

From what I can tell in your code, you’ve stored all category featured images in the category_images option. So you can use get_option to retrieve the featured image for each category:

$image_urls = get_option( 'category_images', array() );    

foreach ( $categories as $category ) {
    $cat_id = $category->term_id;
    $image_url = isset( $image_urls[$cat_id ) ? $image_urls[$cat_id] : '';

    ...
}

You can then use $image_url as the src attribute of an <img>.

How can I display only the subcategories of a certain category parent?

Like get_terms, get_categories has a ‘child_of’ parameter. Again, in your foreach loop:

    $sub_categories = get_categories( array(
        'orderby' => 'name',
        'order' => 'ASC',
        'child_of' => $cat_id,
    ) );

    foreach ( $sub_categories as $sub_category ) {
        // Process the sub-categories here.
    }

Is it possible to display these as a grid?

Yes, you can! But that’s an HTML/CSS question that I think is beyond the scope of this question. Go ahead and open a new question under that topic. You can never ask too many. 🙂

Hope this helps!

Leave a Comment