Home » Display, PHP

Display the Subcategory Without the Parent Category

17 July 2008 No CommentEmail This Post Email This Post

Many people on the Wordpress.org support forum are asking how to do this.

Here is the simple solution I came across. It will allow you to display the title of a child category without displaying the parent category.

Put this code in your template:

PHP:
  1. <?php
  2. $category = get_the_category();
  3. echo $category[1]->cat_name;
  4. ?>

(Adapted from this code at Wordpress.org.)

The [1] tells it not to pull the parent category which would be [0] but instead to pull the child category which is [1]. If your hierarchy contains more layers simply change the number accordingly.

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • NewsVine
  • Propeller
  • Reddit
  • Slashdot
  • SphereIt
  • StumbleUpon
  • Technorati
  • blinkbits
  • BlinkList
  • Furl
  • Ma.gnolia
  • Spurl

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.