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.

Introduction

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 a 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 the 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>' );
?>

Conclusion

The default way to get the linked category (or linked list of categories) for WordPress posts is straightforward. This post showed how to get the category name as a string value.

One comment

Leave a Reply

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