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;
?>


Comments

17 responses to “Display WP Post Category without link”

  1. thank you for that little snippet :)

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

  3. Max:

    You need to use wp_list_categories. It has the ability to hide or show empty categories: http://codex.wordpress.org/Template_Tags/wp_list_categories

  4. Thank you. Just what I was looking for.

  5. What about showing multiple categories? Currently it’s only showing one.

  6. Victoria:

    Can you elaborate? Are your posts in more than one category?

  7. Yes, I have posts in several categories, but it seems that only one is showing up when I use the above code. http://varriaga.com/artwork/ this page, though I don’t have the code inserted right now, just the standard one.

  8. Victoria:

    I would try something like this:


    foreach (get_the_category() as $category){
    echo $category->cat_name;
    }

  9. 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 . ' ';

  10. Thanks, Victoria. Glad it worked out for you.

  11. 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.

  12. Ok I tried it a used this code and it was nice!

  13. 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

  14. 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 :)

  15. Jennifer Saenger

    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!

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

  17. Thanks for the code snippet, it saved me lot of time :)