How to Create Categories and Tags for Photos in WordPress

Contact us to discuss your web project.

It frequently happens that in a web project, we want a classification by categories and tags for the photos. Indeed, for example, in several instances, we had to create photo galleries that were grouped by category. In this article, we will explain how to add an option to create categories and tags for your photos on WordPress. You can then use your knowledge of PHP to make the selection to display to your users.

How do I proceed?

To create categories and tags for photos on WordPress, you can use custom taxonomies or the default category. For this, you have the choice between using a WordPress extension or custom programming. For example, we lean more towards custom development in that it avoids us from facing the constraints of plugins. Nevertheless, below are the details of the two options you have at your disposal.

Create categories and tags for photos on WordPress without a plugin

Here is a step-by-step guide to creating categories and tags for your photos:

1- Create custom taxonomies for categories and tags

a. In the WordPress dashboard, go to “Appearance” > “Theme Editor”.

b. To create a new custom taxonomy for photo categories, click “functions.php” to open the file and add the following code:

c. Then, to create a custom taxonomy for photo labels, add the following code:

2 – Add your categories and tags

Once you have added these codes in the “functions.php” file of your current theme, you will see two new tabs below Media:

  • Photo Categories
  • Photo tags

You can now click on these tabs to add your categories and tags. Alternatively, in the library tab, you have the option to add your data directly to the right of the photo.

3 – Adjust your PHP codes to consider categories and labels

The choice of the mode of functioning will usually depend on the need. Indeed, you may want to create a gallery of photos grouped by categories. Or, you may want to display certain photos on pages based on categories or tags. To achieve this, you need to adapt your code accordingly. For example, one of our projects has a section for a gallery of photos organized by category (see the photo below). For this project, we are using ACF Pro with the gallery option to add photos. So, we will then use the ACF documentation reference code in our PHP code to make the block dynamic. Our procedure is therefore as follows:

  • Create the categories required for the types of hairstyles (Haircuts, coloring, …)
  • Add photos to the Media tab
  • Assign categories to each photo
  • Go to the block tab and add the photos to our gallery.
  • Make a request in our PHP code to display categories that are not empty. Finally, a second request to display the photos of the gallery, add the corresponding categories

Example of gallery code filtered by categories.

We leave you below an example of PHP code for creating categories and tags for photos.

The code above allowed us to obtain the following display. It is important to recall that we used the ACF Pro together with some features we created as part of the project. Therefore, you need to pay attention to the process to better understand how it works.

Some WordPress plugins to filter photos by categories.

Here are some popular WordPress plugins for filtering photos by categories:

  1. Envira Gallery
  2. NextGEN Gallery
  3. Photo Gallery
  4. FooGallery
  5. Justified Image Grid

We also recommend that you search your library for extensions to understand how they work.

In conclusion

Creating a photo gallery with a filter by categories or tags can be relatively easy. If you have web development skills, you can opt for custom development. Otherwise, a plugin can help with this direction. If you would like to work with experts to help you with development, contact us. Moreover, you can visit our code repository to download some interesting examples.


What our clients say about us

“Très bonne expérience”

Garage Ayad

President – Garage Ayad

“PrositeWeb is a very professional company. They have helped me with several projects, and I am very satisfied, especially with their respect for deadlines. They support us throughout all our processes. Thank you. ”

Odigues Joseph

President and founder – Oneevo Immobilier Inc.

“Début 2024, nous avons travaillé avec Prositeweb, Gilblas et ce fût un véritable plaisir et succès. Ça se résume en suivi, communication et compétence. C'est ce qu'on recherche lorsqu'on confie un mandat web (API) personnalisé comme le nôtre. Chapeau, Prositeweb, Gilblas, je le recommande et je retravaillerai avec sans aucun doute.”

Martin Beaudet

Président – Authen Tic

Ce site utilise des cookies pour vous garantir la meilleure expérience sur notre site. En utilisant notre site, vous acceptez les cookies. Apprendre encore plus