Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I apologize if this is a dumb question, but I am just learning PHP and Wordpress. It's probably very simple.

I have a site where the top-level nav is all parent categories, with the child categories as dropdowns. The pages are all pulling from category.php.

On the child category pages, I would like to display the current child category page title (single_cat_title, I think) for styling, but I want to exclude this feature on the parent category pages.

I've been trying to write this very simply using get_the_category() as per this example in the codex:

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

Any help is greatly appreciated.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

Add a check wherever you're displaying the title:

//Anything greater than 0 means the category has parents, 0 means no parent, child category
if($category->category_parent > 0){
    echo $category[0]->cat_name;
}

This should help.

share|improve this answer
    
thanks Rutwick. So I dropped ` $category = get_the_category(); if ($category->category_parent > 0) { ?> <h1><?php $category[0]->cat_name; } ?> </h1>` into my template, but nothing prints. Am I missing something? One thing I realized is that all the posts are assigned to multiple categories - could that be messing it up? (With the appropriate php tags open and close) –  Max Mar 27 '12 at 4:54
    
Try a print_r on $category to check if its right –  Rutwick Gangurde Mar 27 '12 at 5:11
    
Hi Rutwick - thank you again, that helped me work it out. –  Max Mar 29 '12 at 22:11
    
You're welcome! Please accept the answer! ;) –  Rutwick Gangurde Mar 30 '12 at 3:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.