Quick Tip: Post Types, Taxonomies and Permalinks

When it comes to working with WordPress, there's an option under your General Settings for how to change up the permalinks.

A Permalink Primer

Depending on your level of experience with the CMS, then you're already familiar with how this impacts how visitors reach your content. If not, suffice it to say that permalinks are responsible for defining what your URLs—your web addresses—look like when people visit your site.

Some examples include (note that none of these are active links):

  1. http://acme-demo.com/?p=100
  2. http://acme-demo.com/2014/03/21/hello-world/
  3. http://acme-demo.com/hello-world/
  4. http://acme-demo.com/archives/100

The various URL structures that you see above all point to the same post; however, they all look vastly different, right?

In WordPress, there are "pretty permalinks" which is a a colloquial term that references the third example above. Within the WordPress dashboard, this is marked as the "Post Name" setting.

In many cases, though not always, you're going to find people using this particular type of URL scheme for their website especially if it's something more advanced than a blog. That is, bloggers often like to use the year and, optionally, the month and date the post was published to indicate the date on which the post was published.

But I digress on the rest of that topic.

Custom Post Types and Taxonomies

Developers often use custom post types and custom taxonomies when working on projects because it allows them to provide a more cleanly defined conceptual model for how the information is going to be organized.

For example, when it comes to using WordPress, not everything will be a "Post." Instead, you may have a "Post Type" of "Car" on which you can do a write up about a certain type of vehicle.

Similarly, you won't always used categories and/or tags for your posts. Instead, you  may define your own taxonomies by which you can classify your posts. In keeping with the previous example, you may use a term such as "Car" or a subcategory such as "Sedan" to organize your post types.

Whatever the case may be, both custom post types and taxonomies offer the ability to customize the rewrite rules of how the content is reached within the URL structure of WordPress.

In order to be understand this, let's take a look at both of these custom aspects of WordPress and the role of URL rewriting.

Custom Post Types

Assuming that you're working with custom post types and you you're working with pretty permalinks, then you'll need to define your custom post type like this:

First, this example is borrowed directly from the Custom Post Type Codex Article.

In particular, note in the example above that the rewrite parameter is specified. If you're not using pretty permalinks, then you don't need to worry about this; however, if you are using pretty permalinks then this is key to making sure your custom post types render correctly within the context of the rest of your content.

In the example above, your product—say, acme-product—will be accessible via the /products/acme-product/ URL. This is because we've set the slug to be product, and the title of the custom post type would be "Acme Product" resulting in the acme-product URL.

Taxonomies

Just as custom post types offer a way to define the rewrite slug for their content, taxonomies do, too.

But before we go any further, remember that taxonomies are defined as a way to classify information. For example, categories are taxonomies. A specific category, say, "Code" is a taxonomy term.

When we create our own taxonomies, we can also take advantage of custom rewrite rules such that they fit into our pretty permalink scheme as well.

Straight out of the Taxonomy Codex article, let's take a look at the following example:

In this example, we're introducing a new taxonomy—people—that's applicable to the standard post type.

Notice, however, that the slug for People is not people, but it's person. This means that if we want to view a post that is stamped with the People taxonomy, then the URL will read something like /person/john-smith/ where people is the taxonomy and "John Smith" is the name of the post.

We're Only Scratching the Surface

Of course, when it comes to extending WordPress' native functionality with something such as custom post types, taxonomies, and even custom URL rewriting.

Tuts+ has a number of premium courses available for WordPress developers each of which covers custom post types, taxonomies, and other similar functionality; however, sometimes subscribers end up sending questions in that are outside the scope of the the course, or that aren't answered by the content in which they've viewed.

To that end, we'll occasionally offer tips to help clarify some of these points in order to make for a stronger educational experience across the Tuts+ network, and for a stronger development experience with WordPress.

Tags:

Comments

Related Articles