WordPress: Display categories without a link

WordPress code snippets
Published: by
Categories
WordPress

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 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 is 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 bts_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
  bts_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
  bts_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
  bts_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 & discuss

Your comment is important, but don't be a knob. Keep it constructive and polite.

Leave a Reply

Your email address will not be published.

More WordPress related posts

Online micro-electronic components, consumables & accessories
Online micro-electronic components, consumables & accessories
16 January 2021
Ad: Buy great value for money micro-electronic components online from BangGood.com. Free international delivery, including to South Africa. More…
Using the Media Library Assistant plugin for WP
Using the Media Library Assistant plugin for WP
5 January 2019
WordPress websites with a large amount of Media content can become overwhelming to manage. With expanding¬†libraries, images and other media can start to disappear in a sea of files which can make finding them very difficult. With the Media Library Assistant plugin for WordPress, searching and organising Media files becomes a breeze. More…
Categories
WordPress