How to Use the_category Tag Without Links in WordPress

You see, the_category template tag outputs the name of every category a post belongs to, but the category names are linked to respective category pages that I do not want my visitors accessing. Here on GlassOcean.net I try to obscure the category pages that WordPress generates because they are not as customizable as I would like. Therefore, I needed a way to list the categories that a post belongs to without the links.

A Google search for “wordpress the_category without links” returned an article by Robert Mirabito titled How to remove link from the_category in WordPress, which led me to the understanding that I would need to use the get_the_category template tag instead of the the_category template tag. However, Robert’s example inserts a space character as the delimiter, and changing this delimiter to something else results in the delimiter appearing after the very last category. So if you used a comma character as the delimiter, you would end up with something like “Posted in Blog, Services,” which looks bad. This led to some more searching.

A Google search for “wordpress get_the_category separator” returned a question posted by nipponese to the WordPress.org forum titled Adding $separator to get_the_category. Is it possible?

I wasn’t too sure about the solution greenshady provided, so I finally decided it was time to learn how to create my own template tags. One last Google search helped me locate an article by Chris Pearson titled How You Can Use WordPress Functions To Run a Smarter Blog, which had enough information to get me started.

I ended up creating the user-functions.php file and uploading it to my active template folder as described in the article, where I included a custom PHP function that would do the work of displaying the categories how I wanted them.

Check out the code:

<?php
function user_the_categories() {
    // get all categories for this post
    global $cats;
    $cats = get_the_category();
    // echo the first category
    echo $cats[0]->cat_name;
    // echo the remaining categories, appending separator
    for ($i = 1; $i < count($cats); $i++) {echo ', ' . $cats[$i]->cat_name ;}
}
?>

What this little function does is output the first category name, then for every remaining category output a comma and space (‘, ‘), followed by the category name ($cats[$i]->cat_name). We no longer have to worry about that trailing separator character you will most likely have when providing a separator arg with the get_the_categories template tag.

To use this, all I have to do is include this line in my single.php template file:

<?php user_the_categories(); ?>

And what you end up with on the page looks like this:

Posted in Blog, Services

Related

Tags: , ,

9 Comments

  1. Perry, thank you sooo much for this!!! I looked high and low for a way to get my categories to show without hyperlinks. How would you exclude multiple categories in this listing? Thanks in advance!

    Laura

  2. Laura, you pose a useful question. What we need to do is modify the function above to exclude certain categories.

    Here’s the rewritten function:

    function user_the_categories() {
        // get all categories for this post
        global $cats;
        $cats = get_the_category();
        // get all of the category names from $cats
        global $catnames;
        // iterate through each category in $cats
        for ($i = 0; $i < count($cats); $i++) {
            // get the catname for this category
            $catname = $cats[$i]->cat_name;
            // add the catname to the $catnames array as the key AND value
            $catnames[$catname] = $catname;
        }
        // remove any unwanted categories from $catnames by using unset
        unset($catnames["Blog"]);
        // rebuild the array after using unset (important)
        $catnames = array_values($catnames);
        // iterate through the remaining categories in $catnames
        for ($i = 0; $i < count($catnames); $i++) {
            // echo this category
            echo $catnames[$i];
            // if there are more categories remaining, echo a comma separator
            if ($i + 1 != count($catnames)) {echo ', ';}
        }
    }
    
  3. Hi Perry, this is great little tut!
    Is there a way to use this function, but to get reversed output of categories?
    For example, Im getting now it now ordered by name?

  4. this works great! how can I do the same with slugs?

  5. Awesome. This was just what I was looking for! I’m making a custom table for a client and I needed the categories without links. You made it painless =)

  6. Jeff Geidt says:

    Proves that there is really a way around things with the right skills even when it comes to limiting what website visitors can access because true, there are times when you don’t need the links to some categories.

  7. After half the day I found your blog and I want to say thank you this is exactly what I want and needed thank you

  8. Bharat Chowdary says:

    Thanks for the useful code snippet, will use this in my blog 🙂

  9. Thank you! Sometimes we just want to display categories. Is that so much to ask? 😉 The WP team should incorporate something like this into the default args for the_category.

Leave a Reply

Your email address will not be published. Required fields are marked *