0

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.

0

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.

  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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