WordPress: Display categories without a link

WordPress code snippets

The default way to get the linked category (or linked list of categories) for WordPress posts is straight forward, but this post will show how to get the category name as a string value.

This is an ongoing post. Please suggest corrections, explanations, etc. in the comment section at the bottom of this page.

In its simplest form, the the_category() function will display the category name(s) of a post as a link to that category’s archive page. In this case the function is called within The Loop or, by adding the a post ID parameter, it can also be used outside The Loop. By default, the categories will be placed in an unordered list:

<?php // to display an unordered list of post category name(s) as a link
  the_category();
?>

By looking at the the_category() function’s code reference, three optional parameters can be used to change the behaviour the categories are displayed:

<?php
  the_category($separator, $parents, $post_id);
?>

The $separator parameter is commonly used to change the format in which categories are displayed. When left empty, it was mentioned that the default behaviour will be to place the categories in an unordered list. When a separator is added, this behaviour will be changed.

A nice, way to display linked categories are by using a comma as separator:

<?php the_category( ', ' ); ?>

The separator will not be added after the last linked category. By using the following reasoning, each of the linked categories can also be enclosed, for example, in a <div> element:

<div><?php the_category( '</div><div>' ); ?></div>

or to use the linked categories in a sentence:

This post has <?php the_category( ', ' ); ?> as categories.

Display categories without a link

The simplest way to display a category, or a list of categories, without links is by using the following code:

<?php // to display categories without a link
  foreach ((get_the_category()) as $category) {
  echo $category->cat_name . ' ';
  }
?>

which will display the un-linked category, or categories separated by a space.

When only a single category is assigned to the post, or only spaces between un-linked categories are required, the above code will work fine.

A potential problem is that, in the case of multiple categories, there is no way to distinguish between the ‘inter-category separator(s)’ and the ‘last separator’. This means that there will be no control over the formatting (e.g. separators and ability to use <div> elements).

By adding the category count for the post, a while-loop can be used to determine which is the last category:

<?php // to display categories without a link
  $category_counter = count( get_the_terms( $post->ID, 'category' ) );
  $i=0; // counter
  foreach ((get_the_category()) as $category) {
    $i = $i + 1;
    while ( $i < $category_counter ) {
      echo $category->cat_name . ' ';
      break;
    }
  }
  echo $category->cat_name;
?>

Now proper separators can be added. For example, to separate categories with commas and add a full stop to the last un-linked category:

<?php // to display categories without a link
  $cat_before = ''; // use your own
  $cat_separator = ','; // use your own
  $cat_after = '.'; // use your own
  $category_counter = count( get_the_terms( $post->ID, 'category' ) );
  $i=0; // counter
  foreach ((get_the_category()) as $category) {
    $i = $i + 1;
    while ( $i < $category_counter ) {
      echo $cat_before . $category->cat_name . $cat_separator;
      break;
    }
  }
 echo $cat_before . $category->cat_name . $cat_after;
?>

By using the following reasoning, each of the un-linked categories can be enclosed, for example, in a <div> element:

<?php // to display categories without a link
  $cat_before = '<div>'; // use your own
  $cat_separator = '</div><div>'; // use your own
  $cat_after = '</div>'; // use your own
  $category_counter = count( get_the_terms( $post->ID, 'category' ) );
  $i=0; // counter
  foreach ((get_the_category()) as $category) {
    $i = $i + 1;
    while ( $i < $category_counter ) {
      echo $cat_before . $category->cat_name . $cat_separator;
      break;
    }
  }
  echo $cat_before . $category->cat_name . $cat_after;
?>

Creating a function to display categories without a link

If the above code snippet is going to be used frequently, it can be made universal by adding the following function code to the (child) theme’s functions.php file:

// Function to display categories without a link
function cats_without_links( $cat_bef, $cat_sep = ' ', $cat_after, $post ) {
  $category_counter = count( get_the_terms( $post->ID, 'category' ) );
  $i=0; // counter
  foreach ((get_the_category($post->ID)) as $category) {
    $i = $i + 1;
    while ( $i < $category_counter ) {
      echo $cat_bef . $category->cat_name . $cat_sep;
      break;
    }
  }
 echo $cat_bef . $category->cat_name . $cat_after;
}

This function will need four (optional) attributes:

<?php cats_without_link( '$cat_bef', '$cat_sep', 'cat_after', '$post' ); ?>
  • $cat_bef (string) -> what goes before the categories
  • $cat_sep (string) -> what goes between the categories (default = ‘ ‘)
  • $cat_after (string) -> what goes after the categories
  • $post -> the post ID

To call the function from The Loop, using the default attributes, use:

<?php
  cats_without_links();
?>

which will display the un-linked categories separated with a space.

By using the same function, the un-linked categories can also be enclosed, for example, in a <div> element:

<?php cats_without_link( '<div>', '</div><div>', '</div>' ); ?>
About the author
Renier busies himself with improving his English writing, creative web design and his websites, photoshopping, micro-electronics, multiple genres of music, superhero movies and badass series.
Behind the Scenes is a free, informative website. If you find value in any of our content, please consider making a donation to our cause.
Donate via PayPal

Save, share & Disqus

Use the buttons below, on the left or the bottom of this page to share this post. Your comment is important, but don't be a knob. Keep it constructive and polite.

Comment via Disqus

Disqus is a worldwide comment hosting service for web sites and online communities. This secure platform ensures a pleasant commenting environment which is manageable from one account. Use the Login button to sign up.

More website development related posts

Discover the easy way to send bulk SMS from your PC, Mac or mobile device
Discover the easy way to send bulk SMS from your PC, Mac or mobile device
15 October 2019
Ad: Using the latest in technology, WinSMS offers cost effective messaging solutions that makers and business owners can use. With their online portal and API functionality, buying SMS bundles and sending them to recipients is more cost effective and easy to do. More…
Using Cron to schedule tasks
Using Cron for scheduling tasks
30 June 2017 | Updated 11 July 2017
Cron is an Unix-like operating system software utility used to schedule automatic time-based commands. Entered commands can be in the form of Bash or Bash scripts that are meant to be automatically executed periodically at fixed times, dates or intervals. More…