Taxonomy Terms Grid in Oxygen

This members-only tutorial provides the steps to display a grid of taxonomy terms with term image and title appearing for each item in the grid when using Oxygen.

We shall set each item to link to the term archive page.

Update on 13 Mar 2022: Added instructions to set this up for WooCommerce product categories.

Use Case

Let’s say you have a CPT called deal with an associated city taxonomy so that deals can be organized by city. Now you want to have a page that shows all the cities so visitors can go to city of their choice to see deals specific to that city.

This tutorial shows you how we can add obtain all the cities and loop over them to show the city image plus name and use CSS Grid to arrange them in a grid.

Step 1

Let’s register a custom image size for the city images.

Install and activate Code Snippets plugin.

Go to Snippets > Add New.

Title: Custom image sizes

Code:

add_image_size( 'square-thumbnail', 600, 600, true );

Step 2

If you have not already, register deal CPT and associated city taxonomy using a plugin like Custom Post Type UI.

Step 3

Let’s install a plugin so we can assign images to the city taxonomy’s terms.

This is a premium members-only content.

To view the rest of the content, please sign up for membership ($47/month or $599 one-time).

Already a member? Log in below or here.


Posted

in

, ,

by

Comments

2 responses to “Taxonomy Terms Grid in Oxygen”

  1. Famarast Avatar
    Famarast

    Fantastic tutorial, but I would like to use an ACF field instead of WP Term Images, how can I do it?

    I saw your other guide https://wpdevdesign.com/categories-grid-in-oxygen/
    but I was unable to combine the two solutions, what should I change ?.
    Many thanks in advance!

    1. Sridhar Katakam Avatar

      Try replacing

      // image id is stored as term meta.
      $image_id = get_term_meta( $term->term_id, 'image', true );
      
      // image data stored in array, second argument is which image size to retrieve.
      $image_data = wp_get_attachment_image_src( $image_id, 'square-thumbnail' );
      
      // image url is the first item in the array (aka 0).
      $image = $image_data[0];
      
      if ( ! empty( $image ) ) {
          $image_html = sprintf( '<img src="%s" alt="%s" />', esc_url( $image ), $term->name );
      } else {
          $image_html = '';
      }
      

      with

      $image = get_field( 'term_image', $term );
      
      if ( ! empty( $image ) ) {
          $image_html = sprintf( '<img src="%s" alt="%s" />', $image['sizes'][ 'square-thumbnail' ], esc_attr( $term->name ) );
      } else {
          $image_html = '';
      }
      

      where term_image is the name of your custom field.

Leave a Reply

Your email address will not be published. Required fields are marked *