WordPress Function the_tags Explained Using an Example

WordPress Function the_tags Explained Using an Example

June 17, 2020

Per the WordPress codex the usage for the WordPress function for tags is as follows:

<?php the_tags( $before, $sep, $after ); ?>

If you’re new to WordPress theme development like me, this may come off a little confusing.

Let’s help make this easier to understand by using an example.

<?php the_tags('<span class="tag">', '</span><span class="tag">', '</span>'); ?>

In the above example, before each tag begins an opening span tag <span class=”tag”> is added. We’re giving each tag a class of tag for styling purposes.

Each tag is then separated with it’s closing tag </span> and another opening span tag <span=”tag”>.

After all tags have been separated, the last tag has a final closing span tag </span> appended to the end of it.

Here’s How This Code Would Look If It Were Hardcoded

Let’s say we have three tags in one post. These tags are given the names Life Hacks, Productivity, Goals.

Here’s what that code would look like if it were written out line-by-line using the example above:

<span class="tag">Life Hacks</span>
<span class="tag">Productivity</span>
<span class="tag">Goals</span>

Make sense?

Breaking the_tags Down Further

It’s okay if it all doesn’t click with you right away.

Let me break it down again with some labels and arrows!

Ok, so remember, this is the usage for the_tags function:

<?php the_tags( $before, $sep, $after ); ?>

Now let’s break it down 🤘🏻

the_tags WordPress function explained
Graphic made using Excalidraw <3

Boom! I hope this example explanation of the WordPress function the_tags has helped you better understand how the_tags parameters work.