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.
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.
Here is a step-by-step guide to creating categories and tags for your photos:
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:
Once you have added these codes in the “functions.php” file of your current theme, you will see two new tabs below Media:
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.
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:
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.
Here are some popular WordPress plugins for filtering photos by categories:
We also recommend that you search your library for extensions to understand how they work.
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.