Hostingheaderbarlogoj
Join InMotion Hosting for $3.49/mo & get a year on Tuts+ FREE (worth $180). Start today.
Advertisement

Advanced Use of Attachments in WordPress: Assigning Categories and Taxonomy Terms to Attachments

by
Gift

Want a free year on Tuts+ (worth $180)? Start an InMotion Hosting plan for $3.49/mo.

This post is part of a series called Advanced Use of Attachments in WordPress.
Advanced Use of Attachments in WordPress: Creating Custom Queries for Attachments

As mentioned in the article teaser, this tutorial is the first in a four part series in which you'll learn some techniques for working with images in attachments in WordPress which give you advanced options.

In this series, I'll cover:

  • assigning categories and taxonomies to attachments,
  • querying media files by category/taxonomy so you can output them in a custom loop,
  • using taxonomy/category queries for images to display them on archive pages
  • adding an image to a category or taxonomy term as the category or term's 'featured image'

Assigning categories, tags or terms to attachments is a technique that isn't immediately obvious, but attachments are just a post type like any other, so the concept is relatively easy to grasp. Doing this gives you a range of options for managing images and media files in your site and displaying them to users.

Some examples of how you might use this include:

  • You may create a taxonomy for documents and assigning terms to documents as you upload them, then creating a custom taxonomy template file to display a list of documents with a particular term which might be useful for a site which is being used a document management repository.
  • Perhaps you may use a taxonomy to display images with particular terms in a custom taxonomy template file, thus creating galleries without having to go to the lengths of manually creating them.
  • Maybe you have the need to assign a category, tag or taxonomy term to an image to effectively make that image the featured image for the term enabling you to display it in the archive for that term.

The starting point is to create a taxonomy for attachments and assign it to them, so I'll show you how to do that in this tutorial. I'm going to create two taxonomies: one for documents and another for images.

Note: If you want to apply existing categories and tags to your media, see my tutorial on assigning categories and tags to attachments.

What You'll Need

To follow this tutorial you'll need the following:

  • a development installation of WordPress
  • FTP access (or MAMP or similar if you're working locally)
  • a code editor

I'm going to create a plugin for this tutorial as registering taxonomies is theme-independent - but you might choose to put the code in your theme's functions file.

Setting up the Plugin

First, I'll start by setting up my plugin:

<?php
/*
Plugin Name: Tuts+ Advanced use of attachments in WordPress Part 1 - Taxonomies
Plugin URI: http://rachelmccollin.co.uk
Description: This plugin supports the tutorial in wptutsplus. It creates two taxomomies for attachments.
Version: 1.0
Author: Rachel McCollin
Author URI: http://rachelmccollin.com
License: GPLv2
*/
?>

Now I'll create a function for my taxonomies, which I'll hook into the init hook:

function wptp_register_attachments_tax() {

}
add_action( 'init', 'wptp_register_attachments_tax', 0 );

The next step is to populate this function with the code to register my taxonomies, using the register_taxonomy() function.

Registering the Taxonomies

First, I'll register a taxonomy for documents. Add the following inside the wptp_register_attachments_tax() function:

/* register the document catgeories taxonomy */
register_taxonomy( 'document-category', 'attachment',
	array(
		'labels' =>  array(
			'name'              => 'Document Categories',
			'singular_name'     => 'Document Category',
			'search_items'      => 'Search Document Categories',
			'all_items'         => 'All Document Categories',
			'edit_item'         => 'Edit Document Categories',
			'update_item'       => 'Update Document Category',
			'add_new_item'      => 'Add New Document Category',
			'new_item_name'     => 'New Document Category Name',
			'menu_name'         => 'Document Category',
		),
		'hierarchical' => true,
		'sort' => true,
		'show_admin_column' => true
	)
);

Next, let's register the taxonomy for galleries. Add the following below the register_taxonomy() function for the Document Categories taxonomy, still inside the wptp_register_attachments_tax() function:

register_taxonomy( 'gallery-category', 'attachment',
	array(
		'labels' =>  array(
			'name'              => 'Gallery Categories',
			'singular_name'     => 'Gallery Category',
			'search_items'      => 'Search Gallery Categories',
			'all_items'         => 'All Gallery Categories',
			'edit_item'         => 'Edit Gallery Categories',
			'update_item'       => 'Update Gallery Category',
			'add_new_item'      => 'Add New Gallery Category',
			'new_item_name'     => 'New Gallery Category Name',
			'menu_name'         => 'Gallery Category',
		),
		'hierarchical' => true,
		'sort' => true,
		'show_admin_column' => true
	)
);

This adds both taxonomies to the media menu:

advanced-use-of-attachments-in-wordpress-part-1-media-menu-both-taxonomies

Adding Media and Assigning Taxonomy Terms

The final step is to upload some media files and assign taxonomy terms to them. Until you have a range of documents and media with different categories, do this using the WordPress Media Manager.

Because I added the show_admin_column argument to my register_taxonomy() functions, I can see the categories displayed in the Media Library screen. 

Here are the images:

advanced-use-of-attachments-in-wordpress-part-1-media-library

Summary

Assigning taxonomies to attachments is straightforward, and gives you a range of ways you can then display your attachments to users. In this tutorial you've learned how to create the taxonomies and apply them to attachments.

In the next tutorial, I'll demonstrate how to create a custom template file to display attachments using the Document Categories taxonomy created in this tutorial.

Advertisement