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

20 Comments so far

  1. [...] to Corey Salzano for that trick! Did this tip help you? If so, feel free to make a small [...]

  2. [...] – Tactical Tehnique Curiosidades de [...]

  3. [...] original de Corey Salzano Partilha esta [...]

  4. dip on June 7th, 2009

    thank you for that little snippet :)

  5. [...] 13. Display WP Post Category without link [...]

  6. Max on August 19th, 2010

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

  7. Corey on August 24th, 2010

    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

  8. Gemma on February 16th, 2011

    Thank you. Just what I was looking for.

  9. Victoria on February 17th, 2011

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

  10. Corey on February 17th, 2011

    Victoria:

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

  11. Victoria on February 17th, 2011

    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.

  12. Corey on February 17th, 2011

    Victoria:

    I would try something like this:


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

  13. Victoria on February 18th, 2011

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

  14. Corey on February 18th, 2011

    Thanks, Victoria. Glad it worked out for you.

  15. Ohana on February 25th, 2011

    easy like stealing candy from the baby

  16. Ohana on February 25th, 2011

    how can i post category of a post?

  17. Michael on June 16th, 2011

    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.

  18. John on December 11th, 2011

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

  19. Karen on December 12th, 2011

    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

  20. Luka Savic on December 18th, 2011

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

Leave a reply

 

Thanks for reading!

Sign up for email updates: