Display WP Post Category without link

Here is a small piece of code that will display the category name of a WordPress post without a hyperlink to the category page. Typically, the category data is retrieved with the_category( ). This function is not useful for manipulating the category name in plain text. Displaying the category in plain text is easy with get_the_category( ), however.


<?php
$category = get_the_category();
echo $category[0]->cat_name;
?>

17 comments

  1. I need to show categories, even when there are no posts in the category. Currently it hides category with no post.

  2. That worked great, thank you! :]

    Just on a note, I also modified it to show spaces (in case anyone is interested).


    echo ' ' . $category->cat_name . ' ';

  3. Thanks so much for this. I searched Google for hours trying to find this solution. I added a bit of code to it so each category is displayed in a seperate DIV with a grey background. This makes each category display in its own little box.

    Posted in
    <?php
    foreach (get_the_category() as $category){
    echo "<div>";
    echo $category->cat_name;
    echo "</div>";
    }
    ?>

    Thanks again and also to everyone for the helpful comments.

  4. I am using your Twitter plug-in as a widget in my sidebar and I would like to style the output. I can open the CSS stylesheet you have included, but I am not able to make any modifications.

    I am wondering if there is a problem with this, or perhaps I am doing something wrong.

    Thanks!
    Karen

  5. Thanks for this article it saved me some time. I also created simple function that allows using separators( like commas…). Just paste this function in your functions.php and call it in your theme.

    function the_cat_name($separate = “, “) {
    global $post;
    $cat = get_the_category($post->ID);
    $count_cat = count($cat);
    $current_cat_num = 1;
    foreach($cat as $c) {
    $separator = $current_cat_num != $count_cat ? $separate : “”;
    echo $c->cat_name . $separator;
    $current_cat_num++;
    }
    }

    I hope this block of code will help someone :)

  6. Luka,

    Thanks for the function! Cuts down on quite a bit of file size, since I needed to show non-linked categories in several places!

  7. Thank you, Luka! That is exactly what I have been searching for for an hour! This is great.

Comments are closed.